Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cookbook] Idea of article: "How to create before and after filters" #649

Closed
Gregwar opened this issue Aug 18, 2011 · 14 comments
Closed

[Cookbook] Idea of article: "How to create before and after filters" #649

Gregwar opened this issue Aug 18, 2011 · 14 comments
Milestone

Comments

@Gregwar
Copy link

Gregwar commented Aug 18, 2011

This idea come from the following symfony's issue :
symfony/symfony#1975

And the @stof implementation :
symfony/symfony#1975 (comment)

@cordoval
Copy link
Contributor

has someone taken hands on on this yet? nothing has been posted on http://www.ricardclau.com website yet either.

@Gregwar
Copy link
Author

Gregwar commented Aug 24, 2011

@stof What do you think about that:

First, adds to Acme\DemoBundle\Filters\Before.php:

<?php
namespace Acme\DemoBundle\Filters;

/**
 * @Annotation
 */
class Before 
{
    private $methods;

    public function __construct(array $methods)
    {   
        $this->methods = $methods;
    }   

    public function getMethods()
    {   
        return $this->methods;
    }   
}

Add to your services:

before_controller.listener:
    class: Acme\DemoBundle\EventListener\BeforeControllerListener
    arguments: [@annotation_reader]
    tags:
        - { name: kernel.event_listener, event: kernel.controller, method: onKernelController }

Then adds the listener:

<?php
namespace Acme\DemoBundle\EventListener;

use Symfony\Component\HttpKernel\Event\FilterControllerEvent;

class BeforeControllerListener
{
    private $annotation_reader;

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

    public function onKernelController(FilterControllerEvent $event)
    {   
        $controller = $event->getController();
        if (!is_array($controller)) {
            // not a object but a different kind of callable. Do nothing
            return;
        }   

        $controllerObject = $controller[0];
        $class = new \ReflectionClass(get_class($controllerObject));

        if ($before = $this->annotation_reader->getClassAnnotation($class, 'Acme\DemoBundle\Filters\Before')) {
            foreach ($before->getMethods() as $method) {
                $class->getMethod($method)->invoke($controllerObject);
            }   
        }   
    }   
}

You can then add your before filters annotating your class:

<?php
namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

/**
 * @Before("beforeFilter")
 */
class WelcomeController extends Controller
{
  public function beforeFilter()
  {
    // Do something
  }
}

That would be also interresting to show how an annotation can be created in a simple way

@weaverryan
Copy link
Member

This still seems like a pretty cool idea for a cookbook article - a good combination of listeners and other topics for a fairly good use-case. So, if anyone wants to tackle this... :)

@ricardclau
Copy link
Contributor

I did a couple of posts about that subject some months ago when I started working with Symfony2 and needed a before filter. You can see them at:

http://www.ricardclau.com/2011/08/kohana-style-before-and-after-methods-in-symfony2/
http://www.ricardclau.com/2011/09/kohana-style-before-and-after-methods-in-symfony2-vol-ii/

If you feel this could be interesting I can work on a cookbook version of that stuff. Most PHP developers coming from other frameworks look for this feature and it is not that clear when you are starting how to do so.

@weaverryan up to you! I will be more than glad to collaborate!

@stof
Copy link
Member

stof commented May 16, 2012

@ricardclau yeah, please turn it into a cookbook entry (my only suggestion right now would be to inject only the needed parameters instead of injecting the whole container in the listener)

@ricardclau
Copy link
Contributor

Sure, this was one of the points to be changed :)

@gnugat
Copy link

gnugat commented May 23, 2012

Coming from sf1, I've been using this solution for the wrong purpose.
When you try to create a controller with a beforeFilter and extend it in your whole application (which does not work) to use logic before everything else, application wide, you might want to use instead an event listener on the kernel.
A page might fire many kernel events, so make sure you check if the type is MASTER_REQUEST (as seen here: http://stackoverflow.com/questions/7414243/symfony2-help-please-with-backward-uri-referrer-during-switching-locale).

I'm posting it here, because I think it deserves a place as a note in this future cookbook article.

@ricardclau
Copy link
Contributor

Sorry about not coming back to this... I though it was easier to write a cookbook and it is taking longer than I expected!

I hope to send a first draft before weekend and I would like to know if there is any way to test my .rst before actually making the pull request

Any help / comments on that will be much appreciated!

@stof
Copy link
Member

stof commented May 30, 2012

@ricardclau
Copy link
Contributor

This is exactly what I was looking for... amazing as always! Thx @stof!

weaverryan added a commit that referenced this issue Jun 25, 2012
first draft cookbook before/after listeners (issue #649)
@Sgoettschkes
Copy link
Contributor

It looks like both the before and after event listeners are integrated in the cookbook: http://symfony.com/doc/current/cookbook/event_dispatcher/before_after_filters.html#after-filters-with-the-kernel-response-event

If there is nothing else, this issue can be closed @weaverryan

@ricardclau
Copy link
Contributor

Well, it was an idea to document a kernel.request case, obviously not a best case of use, absolutely agree to close it... indeed I thought it was already closed!

Best regards!

@wouterj
Copy link
Member

wouterj commented Jan 23, 2013

@ricardclau you know that you are commenting on the wrong PR and that your PR isn't closed yet?

@ricardclau
Copy link
Contributor

hahaha oh sorry! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants