The Container is storage of services. Any class can be added as a service. The concept of services is very useful because once you add a service, you can get the same instance of the service everywhere within the Webiik application.
โ ๏ธ Container makes the backbone of every Webiik application. Give it a time to understand it perfectly.
You can define services in two files:
private/config/container/services.php
private/config/container/models.php
string $name => callable $factory
for example:
'Name\Space\ClassName' => function (\Webiik\Container\Container $c) {
return new \Name\Space\ClassName();
},
Webiik adds service definitions using the method addService(string $name, callable $factory): void.
๐ Webiik supports language-related service definition files, for example: services.en.php
. Webiik always loads only one version of each service definition file with the following priority: *.en.php
, *.php
.
โ ๏ธ The service name can be custom. However, if you want to use automatic dependency injection into middleware and controllers, you have to follow one of these naming conventions:
Name your service according to class name the service returns incl. the class namespace, for example:
'Webiik\Flash\Flash' => function (\Webiik\Container\Container $c) {
return new \Webiik\Flash\Flash();
},
Name your service to match the following regex ws[A-Z]
, for example:
'wsFlash' => function (\Webiik\Container\Container $c) {
return new \Webiik\Flash\Flash();
},
If you use any configuration values inside your service definition, it can be a good idea to place these values into a separate file. The separate configuration file allows you to configure your service according to the environment and/or language.
private/config/resources.php
.services
. In case your service is a model, add configuration under key models
:
'ClassName' => [
'customMethod' => 'customValue',
],
Key name can be custom. However, itโs a good idea to set a key name similar to the service name, service method name or service parameter name.
๐จโ๐ป๐ Webiik supports local and language related configuration of services. For example: resources.en.php
, resources.en.local.php
. Webiik always loads only one service configuration file with the following priority: *.en.local.php
, *.local.php
, *.en.php
, *.php
. Never publish your local configuration file to production. If you deploy your Webiik project using the Git, Webiik ignores local configuration files, so you donโt have to take care.
All services are stored in the Container. You can get services directly from Container, or you can access services from the constructor in middleware or route controllers.
Usually you will access services directly from Container within service definition. For example:
'Webiik\Translation\Translation' => function (\Webiik\Container\Container $c) {
// Get service Webiik\Arr\Arr from Container
$arr = $c->get('Webiik\Arr\Arr');
// Return the service Webiik\Translation\Translation
return new \Webiik\Translation\Translation($arr);
},
If you name your service by class name, you can access the service by class name from constructor in middleware, model, or route controller:
public function __construct(\Webiik\Flash\Flash $array)
{
$this->flash = $flash;
}
Container will search for the service with name Webiik\Flash\Flash
.
If you name your service with short name, you can access the service by short name from constructor in middleware, model, or route controller:
use Webiik\Flash\Flash as wsFlash;
wsFlash
.