2017 © Pedro Peláez
 

library slim-bridge

PHP-DI integration in Slim

image

php-di/slim-bridge

PHP-DI integration in Slim

  • Saturday, February 24, 2018
  • by mnapoli
  • Repository
  • 6 Watchers
  • 91 Stars
  • 110,657 Installations
  • PHP
  • 32 Dependents
  • 0 Suggesters
  • 19 Forks
  • 4 Open issues
  • 16 Versions
  • 19 % Grown

The README.md

PHP-DI integration with Slim

This package configures Slim to work with the PHP-DI container., (*1)

Build Status , (*2)

The full documentation is here: http://php-di.org/doc/frameworks/slim.html, (*3)

Why?

PHP-DI as a container

The most obvious difference with the default Slim installation is that you will be using PHP-DI as the container, which has the following benefits:, (*4)

If you want to learn more about all that PHP-DI can offer have a look at its introduction., (*5)

Controllers as services

While your controllers can be simple closures, you can also write them as classes and have PHP-DI instantiate them only when they are called:, (*6)

class UserController
{
    private $userRepository;

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function delete($request, $response)
    {
        $this->userRepository->remove($request->getAttribute('id'));

        $response->getBody()->write('User deleted');
        return $response;
    }
}

$app->delete('/user/{id}', ['UserController', 'delete']);

Dependencies can then be injected in your controller using autowiring, PHP-DI config files or even annotations., (*7)

Controller parameters

By default, Slim controllers have a strict signature: $request, $response, $args. The PHP-DI bridge offers a more flexible and developer friendly alternative., (*8)

Controller parameters can be any of these things:, (*9)

  • the request or response (parameters must be named $request or $response)
  • route placeholders
  • request attributes
  • services (injected by type-hint)

You can mix all these types of parameters together too. They will be matched by priority in the order of the list above., (*10)

Request or response injection

You can inject the request or response in the controller parameters by name:, (*11)

$app->get('/', function (ResponseInterface $response, ServerRequestInterface $request) {
    // ...
});

As you can see, the order of the parameters doesn't matter. That allows to skip injecting the $request if it's not needed for example., (*12)

Route placeholder injection

$app->get('/hello/{name}', function ($name, ResponseInterface $response) {
    $response->getBody()->write('Hello ' . $name);
    return $response;
});

As you can see above, the route's URL contains a name placeholder. By simply adding a parameter with the same name to the controller, PHP-DI will directly inject it., (*13)

Request attribute injection

$app->add(function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
    $request = $request->withAttribute('name', 'Bob');
    $response = $handler->handle($request);
    return $response;
});

$app->get('/', function ($name, ResponseInterface $response) {
    $response->getBody()->write('Hello ' . $name);
    return $response;
});

As you can see above, a middleware sets a name attribute. By simply adding a parameter with the same name to the controller, PHP-DI will directly inject it., (*14)

Service injection

To inject services into your controllers, you can write them as classes. But if you want to write a micro-application using closures, you don't have to give up dependency injection either., (*15)

You can inject services by type-hinting them:, (*16)

$app->get('/', function (ResponseInterface $response, Twig $twig) {
    return $twig->render($response, 'home.twig');
});

Note: you can only inject services that you can type-hint and that PHP-DI can provide. Type-hint injection is simple, it simply injects the result of $container->get(/* the type-hinted class */)., (*17)

Documentation

The documentation can be read here: http://php-di.org/doc/frameworks/slim.html, (*18)

The Versions

24/02 2018

dev-master

9999999-dev

PHP-DI integration in Slim

  Sources   Download

MIT

The Requires

 

The Development Requires

24/02 2018

2.0.0

2.0.0.0

PHP-DI integration in Slim

  Sources   Download

MIT

The Requires

 

The Development Requires

24/02 2018

v6.x-dev

6.9999999.9999999.9999999-dev

PHP-DI integration in Slim

  Sources   Download

MIT

The Requires

 

The Development Requires

28/12 2017

1.1.2

1.1.2.0

PHP-DI integration in Slim

  Sources   Download

MIT

The Requires

 

The Development Requires

29/11 2017

dev-revert-31-errorHandler-outputBuffering

dev-revert-31-errorHandler-outputBuffering

PHP-DI integration in Slim

  Sources   Download

MIT

The Requires

 

The Development Requires

15/11 2017

1.1.1

1.1.1.0

PHP-DI integration in Slim

  Sources   Download

MIT

The Requires

 

The Development Requires

14/11 2017

1.1.0

1.1.0.0

PHP-DI integration in Slim

  Sources   Download

MIT

The Requires

 

The Development Requires

03/09 2016

1.0.3

1.0.3.0

PHP-DI integration in Slim

  Sources   Download

MIT

The Requires

 

The Development Requires

06/08 2016

1.0.2

1.0.2.0

PHP-DI integration in Slim

  Sources   Download

MIT

The Requires

 

The Development Requires

01/06 2016

1.0.1

1.0.1.0

PHP-DI integration in Slim

  Sources   Download

MIT

The Requires

 

The Development Requires

08/03 2016

1.0.0

1.0.0.0

PHP-DI integration in Slim

  Sources   Download

MIT

The Requires

 

The Development Requires

30/01 2016

0.2.1

0.2.1.0

PHP-DI integration in Slim

  Sources   Download

MIT

The Requires

 

The Development Requires

23/01 2016

0.2.0

0.2.0.0

PHP-DI integration in Slim

  Sources   Download

MIT

The Requires

 

The Development Requires

10/01 2016

dev-puli

dev-puli

PHP-DI integration in Slim

  Sources   Download

MIT

The Requires

 

The Development Requires

05/01 2016

0.1.1

0.1.1.0

PHP-DI integration in Slim

  Sources   Download

MIT

The Requires

 

The Development Requires

28/12 2015

0.1.0

0.1.0.0

PHP-DI integration in Slim

  Sources   Download

MIT

The Requires

 

The Development Requires