, (*1)
Slim-Container-Builder
A tool to build your Slim Framework 3 container. Instead of having to define your container in php, you just define it from a configuration file., (*2)
This will not replace the pimple
container, but will build one for you., (*3)
Installation
User composer to install, (*4)
composer require flexsounds/slim-container-builder
Install symfony/yaml
to use yaml files!, (*5)
Default usage
This will create a slim framework container with your services defined in a configuration file., (*6)
$containerBuilder = new \Flexsounds\Slim\ContainerBuilder\ContainerBuilder();
$containerBuilder->setLoader($loader = new \Flexsounds\Slim\ContainerBuilder\Loader\FileLoader('./config'));
$loader->addFile("config.yml");
$app = new Slim\App($containerBuilder->getContainer());
// define your routes here.
$app->run();
Configuration file support
We use hassankhan/config to load the configuration, which has support for YML, XML, JSON and some more., (*7)
You can import other configuration files from a configuration file, independent of what file format., (*8)
# config.yml
imports:
- { Resource: otherconfig.xml }
All examples are created in yaml notation!, (*9)
Creating your services
The service configuration has almost the same notation as Symfony
does.
Which means you could copy your service configuration you built to a Symfony application (keep in mind, symfony has many more features than Slim Container Builder
), (*10)
All your services must be defined under the services
key and are shared throughout your entire application.
The default services like settings
, request
, response
, router
etc. from Slim Framework are also available as service, (*11)
Basic example
Creating a service without any arguments is very simple., (*12)
services:
my.custom.service:
class: 'App\MyBasicService'
In your code you can use, (*13)
$app->get('/', function($request, $response){
$myCustomService = $this->get('my.custom.service');
// $myCustomService is now an instance of 'App\MyBasicService'
});
Non-Shared services
All services are shared by default. Which means each time you retrieve the service, you get the same instance.
When you want a new instance each time you call the service, use the shared
key, (*14)
services:
my.nonshared.service:
class: 'App\MyNonSharedService'
shared: false
Dependency Injection
Some of your services might need some dependency injection, this could be done with the arguments
key.
You can inject anyting you want, even other services., (*15)
services:
mailer:
class: 'Location\To\Mailer'
newslettermanager:
class: 'App\Newsletter\NewsletterManager'
arguments:
- '@mailer'
namespace App\Newsletter;
use Location\To\Mailer;
class NewsletterManager {
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
}
Contributing
Read the CONTRIBUTING for details, (*16)