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
Comments
has someone taken hands on on this yet? nothing has been posted on http://www.ricardclau.com website yet either. |
@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:
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 |
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... :) |
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/ 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! |
@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) |
Sure, this was one of the points to be changed :) |
Coming from sf1, I've been using this solution for the wrong purpose. I'm posting it here, because I think it deserves a place as a note in this future cookbook article. |
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! |
This is exactly what I was looking for... amazing as always! Thx @stof! |
first draft cookbook before/after listeners (issue #649)
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 |
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! |
@ricardclau you know that you are commenting on the wrong PR and that your PR isn't closed yet? |
hahaha oh sorry! :) |
This idea come from the following symfony's issue :
symfony/symfony#1975
And the @stof implementation :
symfony/symfony#1975 (comment)
The text was updated successfully, but these errors were encountered: