0

我有这样的路线:

events_print:
    pattern:  /list
    defaults:
        _controller: AcmeCompanyBundle:Events:list
        activeMenu: conferences_events_list

events_edit:
    pattern:  /edit/{id}
    defaults:
        _controller: AcmeCompanyBundle:Events:edit
        activeMenu: conferences_events_edit

在控制器的每个动作中,我都必须这样做:

$menuBuilder = $this->container->get('acme.menu_builder');
$menuBuilder->setActiveMenu($activeMenu);

如果我能以某种方式拦截匹配的路线并setActiveMenu()在一个地方而不是在每个动作中调用,那就太好了。有什么好的方法吗?

更新:我已经稍微简化了这个例子,这样你就不会被不必要的细节弄糊涂了。

4

2 回答 2

1

我终于找到了可能对其他人有用的解决方案。我们可以监听 kernel.controller 事件:

acme.kernel_listener:
    class: Acme\Company\Bundle\EventListener\KernelListener
    arguments:
        container: "@service_container"
    tags:
        - { name: kernel.event_listener, event: kernel.controller, method: onKernelController }

在事件监听器中:

public function onKernelController(FilterControllerEvent $event)
{
    $route = $this->container->get('request')->get('_route');
    if ($routeObj = $this->container->get('router')->getRouteCollection()->get($route)) {
        $defaults = $routeObj->getDefaults();
    }
    // do some common stuff
}

现在我们可以在一个地方完成所有常见的事情。

于 2013-06-19T07:58:46.900 回答
1

我想你需要的是一个控制器监听器

services.yml

services:
    acme_controller.listener:
        class: Acme\CompanyBundle\Listener\ControllerListener
        arguments: [ @acme.menu_builder ]
        tags:
            - { name: kernel.event_listener, event: kernel.controller, method: onKernelController }

ControllerListener.php

<?php

namespace Acme\CompanyBundle\Listener;

use Symfony\Component\HttpKernel\Event\FilterControllerEvent;

/**
 * Controller listener
 */
class ControllerListener
{
    protected $menuBuilder;

    /**
     * Constructor
     *
     * @param Object $menuBuilder A Menu Builder instance
     */
    public function __construct($menuBuilder)
    {
        $this->menuBuilder = $menuBuilder;
    }

    /**
     * Sets the active menu
     *
     * @param  FilterControllerEvent $event
     */
    public function onKernelController(FilterControllerEvent $event)
    {
        $controller = $event->getController();
        $request    = $event->getRequest();

        /**
         * Your logic
         */

        $this->menuBuilder->setActiveMenu($activeMenu);
    }
}
于 2013-06-19T07:59:36.243 回答