Zend Framework 2: get matched route in view

2019-02-08 17:24发布

I'm currently learning ZF2 by developing a small MVC application roughly based on the skeleton app. Right now I'm trying to hide some fixed HTML elements based on the route matched: just as an example, I don't want the main menu to show during the login phase.

I can do that easily by passing toggle parameters as return values from the controller actions, but it doesn't feel right, so I'd like to just check the matched route from the layout and compose the layout accordingly.

Problem is, I don't know how to get the matched route in the template. Is this possible? Are there other solutions to avoid adding layout logic into controllers?

Edit after some good Frankenstein work, I was able to find a solution for this. I like the idea of using a helper, so I just tried to pass it the Application object, from the boostrap function in the main module:

$app = $e->getApplication();
$serviceManager = $app->getServiceManager();
....
$serviceManager->get('viewhelpermanager')->setFactory('getRoute', function($sm) use ($app) {
    return new Helper\GetRoute($app);
});

and the helper function:

use Zend\View\Helper\AbstractHelper;

class GetRoute extends AbstractHelper {
    private $sm;

    public function __construct($app) {
        $this->sm = $app->getServiceManager();
    }

    public function echoRoute() {
        $router = $this->sm->get('router');
        $request = $this->sm->get('request');

        $routeMatch = $router->match($request);
        if (!is_null($routeMatch))
            echo $routeMatch->getMatchedRouteName();
    }
}

perhaps there's a cleaner, more ZF2ish way to do this...

10条回答
趁早两清
2楼-- · 2019-02-08 17:56

Additional information about "Rodrigo Boratto" post for integrating getRouteMatch in ZF3 (I can't comment because I have under 50 repo...)

In view helper file these line:

use Zend\Mvc\Router\RouteMatch as MvcRouteMatch;
use Zend\Mvc\Router\RouteStackInterface;

should be:

use Zend\Router\RouteMatch as MvcRouteMatch;
use Zend\Router\RouteStackInterface;

I don't know when they made that change but the files are in Zend\Router namespace.

P.S. I use composer if that matters.

查看更多
姐就是有狂的资本
3楼-- · 2019-02-08 17:57

I view you can use

$this->getHelperPluginManager()->getServiceLocator()->get('Application')->getMvcEvent()->getRouteMatch()->getMatchedRouteName();
查看更多
手持菜刀,她持情操
4楼-- · 2019-02-08 18:01

I believe you can solve it by finding the action/controller names:

$controller = $this->getRequest()->getControllerName();
$action = $this->getRequest()->getActionName();

Once you know the action, you can have specific conditions to enable sections of the layout.

查看更多
够拽才男人
5楼-- · 2019-02-08 18:02

You could create a View helper that implements ServiceManagerAwareInterface. Then inside the View helper using the ServiceManager instance to retrieve both the router and request objects then reconstruct the route match.

$services = $this->getServiceManager();

$router = $services->get('router');
$request = $services->get('request');

$routeMatch = $router->match($request);
echo $routeMatch->getMatchedRouteName();

I'd also recommend writing the View helper so that code only triggers once per request.

查看更多
登录 后发表回答