MrQ*_*MrQ 4 php navigation routing breadcrumbs zend-framework2
我是Zend Framework 2的新手,我正在尝试使用ZF2生成动态Breadcrumb但没有成功.
我使用http://adam.lundrigan.ca/2012/07/quick-and-dirty-zf2-zend-navigation/作为我工作的基础,如上所述,我为我的模块公开的路径构建了站点地图.
例:
// config/autoload/nav_zfcuser.global.php
<?php
return array(
// All navigation-related configuration is collected in the 'navigation' key
'navigation' => array(
// The DefaultNavigationFactory we configured in (1) uses 'default' as the sitemap key
'default' => array(
// And finally, here is where we define our page hierarchy
'home' => array(
'label' => 'Home',
'route' => 'home',
'pages' => array(
'sitemap' => array(
'label' => 'Sitemap',
'title' => 'Sitemap',
'route' => 'sitemap',
),
'aboutus' => array(...),
'privacy' => array(...),
),
),
),
),
);
Run Code Online (Sandbox Code Playgroud)
然后使用以下视图帮助器呈现面包屑:
<?php echo $this->navigation()->breadcrumbs('Navigation'); ?>
Run Code Online (Sandbox Code Playgroud)
这工作正常,例如,如果我在mywebsite.com/sitemap上导航,渲染的bredcrumbs将是.
主页>网站地图
问题是当我开始使用动态URL时(例如,使用产品ID).
在module.config.php上,路由是..
<?php
return array(
'router' => array(
'routes' => array(
(...)
'productHome' => array(
'type' => "Literal",
'options' => array(
'route' => "/product",
'defaults' => array(
'__NAMESPACE__' => "Application\Controller",
'controller' => "Product",
'action' => "index",
),
),
'may_terminate' => true,
'child_routes' => array(
'product' => array(
'type' => "Segment",
'options' => array(
'route' => "/:idProduct[/:name]",
'defaults' => array(
'__NAMESPACE__' => "Application\Controller",
'controller' => "Product",
'action' => "product",
),
),
'may_terminate' => true,
),
),
Run Code Online (Sandbox Code Playgroud)
上面定义的路由(module.config.php)转换为以下URL结构:
mysite.com/Product/:idProduct[/:name]
因此,构建站点地图以反映此结构应该是:
// config/autoload/nav_zfcuser.global.php
<?php
return array(
// All navigation-related configuration is collected in the 'navigation' key
'navigation' => array(
// The DefaultNavigationFactory we configured in (1) uses 'default' as the sitemap key
'default' => array(
// And finally, here is where we define our page hierarchy
'home' => array(
'label' => 'Home',
'route' => 'home',
'pages' => array(
'sitemap' => array(...),
'productHome' => array(
'label' => 'Product',
'route' => 'product',
'controller' => 'Product',
'action' => 'index',
'pages' => array(
'product' => array(
'label' => 'Product',
'controller' => 'Product',
'action' => 'product',
'route' => 'product/:idProduct[/:name]',
),
),
),
),
),
),
),
);
Run Code Online (Sandbox Code Playgroud)
刷新浏览器上的产品页面mywebsite.com/product/flv6n768/ProductName 不显示面包屑.
如何在调用视图助手时传递'idProduct'和'name'参数
<?php echo $this->navigation()->breadcrumbs('Navigation'); ?>
Run Code Online (Sandbox Code Playgroud)
它呈现:首页>产品> idProduct(不可见)>产品名称?
如果仅出于面包屑的目的,您可以执行以下操作:
修改您的导航阵列如下 - 删除特定的产品页面规范.并将ID添加到产品页面.
<?php
return array(
// All navigation-related configuration is collected in the 'navigation' key
'navigation' => array(
// The DefaultNavigationFactory we configured in (1) uses 'default' as the sitemap key
'default' => array(
// And finally, here is where we define our page hierarchy
'home' => array(
'label' => 'Home',
'route' => 'home',
'pages' => array(
'sitemap' => array(...),
'productHome' => array(
'id' => 'productHome',//<<< Page unique ID
'label' => 'Product',
'route' => 'product',
'controller' => 'Product',
'action' => 'index',
),
),
),
),
),
),
);
Run Code Online (Sandbox Code Playgroud)
在Application\Controller\Product :: product()中添加以下代码.这会将"动态"新子页面添加到导航静态结构中的产品索引页面.另请注意,我假设您已在$ product变量中获得实际的产品实例.
$navigation = $this->getServiceLocator()->get('Navigation');
$page = $navigation->findBy('id', 'productHome');
$page->addPage(array(
'uri' => $this->getRequest()->getRequestUri(),//current page URI
'label' => $product->getName(),//<<<<< product name
'active' => true,
));
Run Code Online (Sandbox Code Playgroud)