The Symple Initializable Controller Bundle permits initialization of Symfony controllers before invocation of action methods.
Content:
- PHP >= 5.5.9
- symfony/config ~3.0
- symfony/dependency-injection ~3.0
- symfony/http-kernel ~3.0
- doctrine/annotations ~1.2
The suggested installation method is via composer:
php composer.phar require symple-dev/init-controller-bundle:1.0.*
You can enable or disable Symple Initializable Controller in app/config.yml (default enabled):
init_controller:
enabled: true
Implement the interface Symple\Bundle\InitControllerBundle\Controller\InitControllerInterface in your Controller.
<?php
namespace Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symple\Bundle\InitControllerBundle\Controller\InitControllerInterface;
class MyController extends Controller implements InitControllerInterface
{
// ...
}
The annotation @Symple\Bundle\InitControllerBundle\Annotation\Init tells that this method should be invoked before some action method (on kernel.controller event).
Available configuration options:
- priority - integer value for invocation ordering (the greatest will be first), default 0
- args - array of method arguments (optional)
Note: that @Init annotation can be applied to public methods only.
<?php
namespace Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symple\Bundle\InitControllerBundle\Annotation\Init;
use Symple\Bundle\InitControllerBundle\Controller\InitControllerInterface;
class MyController extends Controller implements InitControllerInterface
{
/**
* @Init(priority = 200)
*/
public function initialize1()
{
// do something
}
/**
* @Init(priority = 100, args = {123, true})
*/
public function initialize2($int, $bool = false)
{
// do something
}
public function indexAction()
{
// ...
}
}
In this example following methods will be invoked before indexAction (or another action method):
initialize1();
initialize2(123, true);
You can apply multiple @Init annotations to the same method.