flexsounds / slim-symfony-di-container
The bridge to use the Symfony Dependency Injection Container to use in Slim Framework 3
Installs: 48 567
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 1
Open Issues: 1
Requires
- php: ^7.1
- slim/slim: ^3.1
- symfony/config: ^3.2 || ^4.0
- symfony/dependency-injection: ^3.2 || ^4.0
Requires (Dev)
- phpunit/phpunit: ^7.0
- symfony/yaml: ^3.2 || ^4.0
This package is auto-updated.
Last update: 2024-11-27 18:52:51 UTC
README
Slim-Symfony-Dependency-Injection-Bridge
Just a simple bridge to use the Symfony Dependency Injection Container to replace the Container in Slim Framework 3
This will replace the pimple
container which comes default with Slim Framework 3.
The default services (like router
, request
, response
) which Slim Framework uses, are preloaded in the ContainerBuilder
. This way Slim will work as it should.
Installation
Use composer to install
composer require flexsounds/slim-symfony-di-container
Default usage
To use the Symfony DI Container just add the ContainerBuilder to Slim\App
$container = new \Flexsounds\Component\SymfonyContainerSlimBridge\ContainerBuilder(); $app = new \Slim\App($container); // define your routes here. The container is available through $this in the route closure $app->run();
Default parameters
The default Slim Framework Settings are 1 on 1 mapped with parameters. To overwrite the settings you can either create a new ParameterBag
with your settings and pass it as the first argument to the ContainerBuilder
.
Or if you use one of the file loaders, change them with the parameters config key.
Just change the parameters to your own choice to your config file like this.
parameters:
httpVersion: "1.1"
responseChunkSize: 4096
outputBuffering: "append"
determineRouteBeforeAppMiddleware: false
displayErrorDetails: false
Other Examples
Example loading your dependencies through yaml configuration (The Symfony way)
$container = new \Flexsounds\Component\SymfonyContainerSlimBridge\ContainerBuilder(); $loader = new \Symfony\Component\DependencyInjection\Loader\YamlFileLoader($container, new \Symfony\Component\Config\FileLocator(__DIR__)); $loader->load('config.yml'); $app = new \Slim\App($container); $app->run();
Now you can create a config.yml
file to load your services, parameters, etc. The use of importing other config files is also available.
services: my.custom.service: class: Location\To\The\Class
Now the service my.custom.service
is available in the container. Use $this->get('my.custom.service')
to load the service.
$app->get('/', function($request, $response){ $customService = $this->get('my.custom.service'); // $customService is now an instance of Location\To\The\Class() });
Read more
Read the symfony service container documentation to find out what other options are available in the service container.
Read the symfony dependency injection documentation to find out how the ContainerBuilder is used. Like setting default parameters.
Interesting to know
If you use PhpStorm as IDE and add the Symfony Plugin, typehinting for services should be available.