如何在View中获取匹配的路线名称-Zend Expressive [英] How to get matched route name in View - Zend Expressive

查看:66
本文介绍了如何在View中获取匹配的路线名称-Zend Expressive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以生成传递路线名称的URL

I know that I can generate URL passing the route name

<?php echo $this->url('route-name') #in view file ?>

但是我可以获取相反方向的信息吗? 从当前的URL/URI,我需要获取路由名称.

But can I get information in opposite direction? From current URL/URI, I need to get route name.

实际情况是:我有layout.phtml,顶部菜单(html)在哪里. 菜单中的当前链接需要用CSS类标记.因此,我需要的示例是:

Real case is: I have layout.phtml where is the top menu (html). Current link in the menu need to be marked with css class. So, example what I need is:

<?php // in layout.phtml file
  $index_css   = $this->getRouteName() == 'home-page' ? 'active' : 'none'; 
  $about_css   = $this->getRouteName() == 'about'     ? 'active' : 'none'; 
  $contact_css = $this->getRouteName() == 'contact'   ? 'active' : 'none';  
?>

我正在使用快速路线,但是在任何解决方案中我都很有趣.解决方案不必位于View文件中.

I am using fast route, but I am interesting in any solution. Solution doesn't have to be in View file.

推荐答案

根据我的研究,

From my research, there is such information in RouteResult instance in the public method getMatchedRouteName(). The problem is how to reach to this instance from the View.

我们知道我们可以从中间件的__invoke()方法中的Request对象获得RouteResult.

We know that we can get RouteResult, but from the Request object which is in a Middleware's __invoke() method.

public function __invoke($request, $response, $next){
    # instance of RouteResult
    $routeResult = $request->getAttribute('Zend\Expressive\Router\RouteResult');
    $routeName   = $routeResult->getMatchedRouteName();
    // ... 
}

按照@timdev的建议,我们将在现有帮助器中找到灵感 UrlHelper ,并在自定义View Helper中进行几乎相同的实现 .

As @timdev recommended we will find inspiration in existing helper UrlHelper and make almost the same implementation in custom View Helper.

简而言之,我们将创建2个类.

In short we will create 2 classes.

  1. CurrentUrlHelper 与方法 setRouteResult()
  2. CurrentUrlMiddleware __ invoke($ req,$ res,$ next)
  1. CurrentUrlHelper with method setRouteResult() and
  2. CurrentUrlMiddleware with __invoke($req, $res, $next)

我们将在CurrentUrlMiddleware中注入CurrentUrlHelper, 在__invoke()方法中,使用适当的RouteResult实例调用 CurrentUrlHelper :: setRouteResult(). 稍后,我们可以将我们的CurrentUrlHelper与RouteResult实例一起使用.这两个类都应该也有一个工厂.

We will inject the CurrentUrlHelper in CurrentUrlMiddleware and in __invoke() method call the CurrentUrlHelper::setRouteResult() with appropriate RouteResult instance. Later we can use our CurrentUrlHelper with RouteResult instance in it. Both classes should have an Factory too.

class CurrentUrlMiddlewareFactory {
    public function __invoke(ContainerInterface $container) {
        return new CurrentUrlMiddleware(
            $container->get(CurrentUrlHelper::class)
        );
    }
}

class CurrentUrlMiddleware {
    private $currentUrlHelper;

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

    public function __invoke($request, $response, $next = null) {
        $result = $request->getAttribute('Zend\Expressive\Router\RouteResult');
        $this->currentUrlHelper->setRouteResult($result);

        return $next($request, $response); # continue with execution
    }
}

还有我们的新助手:

class CurrentUrlHelper {
    private $routeResult;

    public function __invoke($name) {
        return $this->routeResult->getMatchedRouteName() === $name;
    }

    public function setRouteResult(RouteResult $result) {
        $this->routeResult = $result;
    }
}


class CurrentUrlHelperFactory{
    public function __invoke(ContainerInterface $container){
        # pull out CurrentUrlHelper from container!
        return $container->get(CurrentUrlHelper::class);
    }
}

现在,我们只需要在配置中注册新的View Helper和中间件:

Now we only need to register our new View Helper and Middleware in the configs:

dependencies.global.php

'dependencies' => [
    'invokables' => [
        # dont have any constructor! 
        CurrentUrlHelper::class => CurrentUrlHelper::class, 
    ],
]

middleware-pipeline.global.php

'factories' => [
    CurrentUrlMiddleware::class => CurrentUrlMiddlewareFactory::class,
], 
'middleware' => [
    Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE,
    Zend\Expressive\Helper\UrlHelperMiddleware::class,
    CurrentUrlMiddleware::class,         # Our new Middleware
    Zend\Expressive\Container\ApplicationFactory::DISPATCH_MIDDLEWARE,
],

最后,我们可以在 templates.global.php

'view_helpers' => [
    'factories' => [
        # use factory to grab an instance of CurrentUrlHelper
        'currentRoute' => CurrentUrlHelperFactory::class 
    ]
],

  • 在ROUTING_MIDDLEWARE之后和DISPATCH_MIDDLEWARE之前注册我们的中间件很重要!

    • it's important to register our middleware after ROUTING_MIDDLEWARE and before DISPATCH_MIDDLEWARE!

      此外,我们只有CurrentUrlHelperFactory才能将其分配给键"currentRoute".

      Also, we have CurrentUrlHelperFactory only to assign it to the key 'currentRoute'.

      现在您可以在任何模板文件中使用辅助程序了:)

      Now you can use helper in any template file :)

      <?php // in layout.phtml file
        $index_css   = $this->currentRoute('home-page') ? 'active' : 'none'; 
        $about_css   = $this->currentRoute('about') ? 'active' : 'none'; 
        $contact_css = $this->currentRoute('contact') ? 'active' : 'none';  
      ?>
      

      这篇关于如何在View中获取匹配的路线名称-Zend Expressive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆