Symfony 2: how to sunbathe code before EVERY controller?

I want to check if the user is logged in - before calling the call.

Something like preDispatch()that Symfony\Bundle\FrameworkBundle\Controller\Controllerwould be great.

I would like to avoid :

class MyBaseController extends Controller {
    public function __construct() {
        // ...
        // check: logged in?
        // ...
    }
}

class MyFooController extends MyBaseController() {
    // ...
}

I just found threads from 2011/2012. I'm new to Symfony using version 2.5, and from the very beginning I would like to learn about best practices. Any tips?

Thanks in advance!

Edit

interesting: http://symfony.com/doc/current/cookbook/event_dispatcher/before_after_filters.html

+4
source share
1 answer

1/ ( ) kernel.controller Event. .

2/ , kernel.request t , .

, HttpKernel, (, , , , ).

kernel.controller ( , ). , , , HttpKernel. ,

HttpKernel, , Symfony .

3/ @Security, ,

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;

class MyFooController extends Controller
{
    /**
     * @Security("has_role('ROLE_USER')")
     */
    public function anyAction()
    {
        // ...
+12

All Articles