gpt4 book ai didi

php - zend framework 2 动态面包屑 - 传递参数

转载 作者:可可西里 更新时间:2023-10-31 22:55:46 24 4
gpt4 key购买 nike

我是 Zend Framework 2 的新手,我正在尝试使用 ZF2 生成动态面包屑,但没有成功。

我使用了 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(...),
),
),
),
),
);

然后使用以下 View 助手来渲染面包屑:

<?php echo $this->navigation()->breadcrumbs('Navigation'); ?>

这很好用,例如,如果我在 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,
),
),

上面定义的路由(module.config.php)翻译成下面的URL结构:

mysite.com/Product/:idProduct[/:name]

所以构建站点地图以反射(reflect)这种结构应该是:

// 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]',

),
),
),
),
),
),
),
);

在浏览器上刷新产品页面mywebsite.com/product/flv6n768/ProductName不显示面包屑。

如何传递 'idProduct' 和 'name' 参数,以便在调用 View 助手时

 <?php echo $this->navigation()->breadcrumbs('Navigation'); ?>  

它呈现:首页 > 产品 > 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',
),
),
),
),
),
),
);

在 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,
));

关于php - zend framework 2 动态面包屑 - 传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17884759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com