Skip to content

Slim with Symfony dependency injection container through symfony-way integration

License

Notifications You must be signed in to change notification settings

jangolle/slim-symfony-container

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status codecov PHP from Packagist Packagist

Slim with Symfony DI Container integration

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.

Installation

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

Default usage

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();

Use with Symfony configs

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.

Custom wrapper class

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());

Container access inside app

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;
    }
);

PhpStorm Symfony Plugin support

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!

About

Slim with Symfony dependency injection container through symfony-way integration

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages