Download the PHP package php-di/silex-bridge without Composer
On this page you can find all versions of the php package php-di/silex-bridge. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package silex-bridge
PHP-DI integration with Silex
Installation
Usage
In order to benefit from PHP-DI's integration in Silex, you only need to use DI\Bridge\Silex\Application
instead of the original Silex\Application
.
Here is the classic Silex example updated:
Benefits
Using PHP-DI in Silex allows you to use all the awesome features of PHP-DI to wire your dependencies (using the definition files, autowiring, annotations, …).
Another big benefit of the PHP-DI integration is the ability to use dependency injection inside controllers, middlewares and param converters:
Dependency injection works using type-hinting:
- it can be mixed with request parameters (
$name
in the example above) - the order of parameters doesn't matter, they are resolved by type-hint (for dependency injection) and by name (for request attributes)
- it only works with objects that you can type-hint: you can't inject string/int values for example, and you can't inject container entries whose name is not a class/interface name (e.g.
twig
ordoctrine.entity_manager
)
Controllers as services
With Silex and Pimple, you can define controllers as services by installing the ServiceControllerServiceProvider
and using a specific notation.
With the PHP-DI bridge, you can natively define any type of callable based on services:
- object method:
You will notice above that we give the class name and not an object: PHP-DI will instantiate the instance (and inject dependencies inside it) only if it is used.
Again you will notice that we pass the class name and not an instance. PHP-DI will correctly detect that this is an invokable class and will instantiate it.
Middlewares, route variable converters, error handlers and view handlers
The callable resolution described above (for "controllers as services") applies for registering other Silex objects:
For example you can define a middleware like so and let PHP-DI instantiate it:
Configuring the container
You can configure PHP-DI's container by creating your own ContainerBuilder
and passing it to the application:
Silex service providers
Silex offers several "service providers" to pre-configure some 3rd party libraries, for example Twig or Doctrine. You can still use those service providers with this integration (even though in bigger projects you might want to configure everything yourself).
Here is the example of the TwigServiceProvider:
Since Twig services are registered using a custom name instead of the actual class name (e.g. twig
instead of the Twig_Environment
class name), you cannot inject such dependencies into closures. If you want to inject in controller closures, you can alias entries with PHP-DI:
All versions of silex-bridge with dependencies
php-di/php-di Version ~5.0
php-di/invoker Version ~1.3
silex/silex Version ~1.3
pimple/pimple Version ~1.1