Zend Framework 2:动态导航和面包屑
Zend Framework 2: Dynamic navigation and breadcrumbs
我使用位于 config/autoload
的全局文件在 Zend Framework 2 中进行了导航。我怎样才能在这个全局文件中获取路由参数?
我试过这个:
$helper = new Zend\View\Helper\ServerUrl();
$active_url = $helper->getHost();
但这 return 是整个 url。
例如,我有一个 url 这样的
www.mysite.com/controller/action/id/3
我需要知道id。
我的全局文件return一个导航数组,如下所示:
return array(
'navigation' => array(
'default' => array(
array(
'label' => 'Home',
'route' => 'admin',
'active' => true,
'resource' => 'Admin\Controller\Index',
'privilege' => 'index',
'pages' => array(
'label' => 'My posts ',
'route' => 'admin/default',
'params' => array(
'controller' => 'posts',
'action' => 'edit'
),
'resource' => 'Admin\Controller\Posts',
'privilege' => 'edit',
),
)
)
)
);
只要路由不包含参数,一切都可以正常工作。如果我的 url 包含一个 ID 参数,我的菜单 link 和面包屑只是在没有 ID 的控制器和操作中形成。
所以,而不是:
www.mysite.com/posts/edit/id/12
我的 link 看起来像:
www.mysite.com/posts/edit
您正在寻找的是动态面包屑。将面包屑与路由参数联系起来并不常见,但您可以找到一个有答案的问题 here on stackoverflow.
正如在 the documentation here 中所写,自 ZF2 版本 2.2.0 以来,有一个 'useRouteMatch'
标志,您可以将其设置为 true
.
Starting in version 2.2.0, if you want to re-use any matched route parameters when generating a link, you can do so via the “useRouteMatch” flag. This is particularly useful when creating segment routes that include the currently selected language or locale as an initial segment, as it ensures the links generated all include the matched value.
导航器将使用 RouteMatch
中的 params
而不是手动传递 params
arary。因此,您也可以使用您的 ID。
array(
'label' => 'My posts',
'route' => 'admin/default',
'useRouteMatch' => true
),
注意您说您在 autoload/config
文件夹的 global
文件中定义了导航。最好在 module.config.php
中实现导航,如图 here in this tutorial.
我使用位于 config/autoload
的全局文件在 Zend Framework 2 中进行了导航。我怎样才能在这个全局文件中获取路由参数?
我试过这个:
$helper = new Zend\View\Helper\ServerUrl();
$active_url = $helper->getHost();
但这 return 是整个 url。 例如,我有一个 url 这样的
www.mysite.com/controller/action/id/3
我需要知道id。
我的全局文件return一个导航数组,如下所示:
return array(
'navigation' => array(
'default' => array(
array(
'label' => 'Home',
'route' => 'admin',
'active' => true,
'resource' => 'Admin\Controller\Index',
'privilege' => 'index',
'pages' => array(
'label' => 'My posts ',
'route' => 'admin/default',
'params' => array(
'controller' => 'posts',
'action' => 'edit'
),
'resource' => 'Admin\Controller\Posts',
'privilege' => 'edit',
),
)
)
)
);
只要路由不包含参数,一切都可以正常工作。如果我的 url 包含一个 ID 参数,我的菜单 link 和面包屑只是在没有 ID 的控制器和操作中形成。
所以,而不是:
www.mysite.com/posts/edit/id/12
我的 link 看起来像:
www.mysite.com/posts/edit
您正在寻找的是动态面包屑。将面包屑与路由参数联系起来并不常见,但您可以找到一个有答案的问题 here on stackoverflow.
正如在 the documentation here 中所写,自 ZF2 版本 2.2.0 以来,有一个 'useRouteMatch'
标志,您可以将其设置为 true
.
Starting in version 2.2.0, if you want to re-use any matched route parameters when generating a link, you can do so via the “useRouteMatch” flag. This is particularly useful when creating segment routes that include the currently selected language or locale as an initial segment, as it ensures the links generated all include the matched value.
导航器将使用 RouteMatch
中的 params
而不是手动传递 params
arary。因此,您也可以使用您的 ID。
array(
'label' => 'My posts',
'route' => 'admin/default',
'useRouteMatch' => true
),
注意您说您在 autoload/config
文件夹的 global
文件中定义了导航。最好在 module.config.php
中实现导航,如图 here in this tutorial.