Download the PHP package dunglas/action-bundle without Composer
On this page you can find all versions of the php package dunglas/action-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dunglas/action-bundle
More information about dunglas/action-bundle
Files in dunglas/action-bundle
Package action-bundle
Short Description Symfony controllers, redesigned
License MIT
Homepage https://dunglas.fr/2016/01/dunglasactionbundle-symfony-controllers-redesigned/
Informations about the package action-bundle
DunglasActionBundle: Symfony controllers, redesigned
This bundle is a replacement for the controller system of the Symfony framework and for its command system.
It is as convenient as the original but doesn't suffer from its drawbacks:
- Action and console classes are automatically registered as services by the bundle
- Their dependencies are explicitly injected in the constructor (no more ugly access to the service container) using the autowiring feature of the Dependency Injection Component
- Only one action per class thanks to the
__invoke()
method (but you're still free to create classes with more than 1 action if you want to) - 100% compatible with common libraries and bundles including SensioFrameworkExtraBundle annotations
DunglasActionBundle allows to create reusable, framework agnostic (especially when used with the PSR-7 bridge) and easy to unit test classes.
See https://github.com/symfony/symfony/pull/16863#issuecomment-162221353 for the history behind this bundle.
Note for Symfony >=3.3 users
If you use Symfony at version 3.3 or superior, you do not need to use this bundle as all the features were ported in Symfony. You can learn more about it in the Symfony blog or in the Symfony documentation.
Installation
Use Composer to install this bundle:
composer require dunglas/action-bundle
Add the bundle in your application kernel:
Optional: to use the @Route
annotation add the following lines in app/config/routing.yml
:
If you don't want to use annotations but prefer raw YAML, use the following syntax:
Usage
- Create an invokable class
in the
Action\
namespace of your bundle:
Alternatively, you can create a typical Symfony controller class with several *Action
methods in the Controller
directory
of your bundle, it will be autowired the same way.
There is no step 2! You're already done.
All classes inside Action/
and Controller/
directories of your project bundles are automatically registered as services.
By convention, the service name is the Fully Qualified Name of the class.
For instance, the class in the example is automatically registered with the name AppBundle\Action\Homepage
.
There are other classes/tags supported:
Class Name | Tag automatically added | Directory |
---|---|---|
Command | console.command | Command |
EventSubscriberInterface | kernel.event_subscriber | EventSubscriber |
Twig_ExtensionInterface | twig.extension | Twig |
Thanks to the autowiring feature of the Dependency Injection Component, you can just typehint dependencies you need in the constructor, they will be automatically initialized and injected.
Service definition can easily be customized by explicitly defining a service named according to the same convention:
This bundle also hooks into the Routing Component (if it is available): when the @Route
annotation is used as in the example,
the route is automatically registered: the bundle guesses the service to map with the path specified in the annotation.
Dive into the TestBundle to discover more examples such as using custom services with ease (no configuration at all) or classes containing several actions.
Using the Symfony Micro Framework
You might be interested to see how this bundle can be used together with the Symfony "Micro" framework.
Here we go:
Amazing isn't it?
Want to see a more advanced example? Checkout our test micro kernel.
Configuration
Credits
This bundle is brought to you by Kévin Dunglas and awesome contributors. Sponsored by Les-Tilleuls.coop.
All versions of action-bundle with dependencies
symfony/config Version ^2.8 || ^3.0
symfony/dependency-injection Version ^2.8 || ^3.0
symfony/finder Version ^2.8 || ^3.0
symfony/http-kernel Version ^2.8 || ^3.0