Easily resolve Symfony ContainerBuilder
and setup in it all default Slim Application dependencies if necessary. Loader resolve symfony configuration params and setup slim default settings with params to symfony ParameterBag
.
Library is available on Packagist.
Installation via composer is the recommended way to install it.
Just add this line to required
section of your composer.json
file:
"jangolle/slim-symfony-container": "~1.0"
or just run in console
cd /path/to/your/project
composer require jangolle/slim-symfony-container
You can directly create ContainerBuilder
and use it from scratch to setup with SlimDefaultServicesInjection
like this:
<?php
declare(strict_types=1);
use JanGolle\SlimSymfonyContainer\ContainerManager;
use JanGolle\SlimSymfonyContainer\Loader\SlimDefaultServicesInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
$containerManager = new ContainerManager();
$container = new ContainerBuilder();
$container = $containerManager->resolveContainer($container, new SlimDefaultServicesInjection());
$app = new \Slim\App($container);
//setup routes or something
$app->run();
If you want your symfony configuration files in your project you can actually do something like:
<?php
declare(strict_types=1);
use JanGolle\SlimSymfonyContainer\ContainerManager;
use JanGolle\SlimSymfonyContainer\Loader\SlimDefaultServicesInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
$containerManager = new ContainerManager();
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/path/to/config'));
$loader->load('services.yaml');
$container = $containerManager->resolveContainer($container, new SlimDefaultServicesInjection());
$app = new \Slim\App($container);
//setup routes or something
$app->run();
Your services.yaml
parameters block might look like this:
parameters:
slim.settings.httpVersion: '1.1'
slim.settings.responseChunkSize: 4096
slim.settings.displayErrorDetails: !php/const DEBUG
NOTE: You can override default SLIM settings in your config file with your custom values.. or not, it's up to you :) All SLIM necessary settings will be applied to container injection with your params or not.
If you have your own container that is instance of Symfony\Component\DependencyInjection\ContainerBuilder
and you want to setup it with Slim dependencies you can do like this:
$container = new YourCustomContainer($fullOfParams);//instance of Symfony\Component\DependencyInjection\ContainerBuilder
$container = $containerManager->resolveContainer($container, new SlimDefaultServicesInjection());
or you can just instantiate it with default constructor via ::class
as first arg of resolveContainer
:
$container = $containerManager->resolveContainer(YourCustomContainer::class, new SlimDefaultServicesInjection());
Inside your routes scope $this
will return our container which is actually instance of Symfony\Component\DependencyInjection\ContainerBuilder
//...
$app->get(
'/your/route',
function (Request $request, Response $response, array $args) {
$this->get('someService')->doSomeStuff();
return $response;
}
);
If you are using PhpStorm IDE you can install in it Symfony plugin
and get access to typehinting and IDE autocomplite for services.
Thanks for your attention!