草庐IT

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

coder 2024-04-15 原文

我是 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/

有关php - zend framework 2 动态面包屑 - 传递参数的更多相关文章

  1. ruby - rails 3 redirect_to 将参数传递给命名路由 - 2

    我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use

  2. ruby-on-rails - 如何生成传递一些自定义参数的 `link_to` URL? - 2

    我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些

  3. ruby - 在 Ruby 中按名称传递函数 - 2

    如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只

  4. ruby - 如何将 Puma::Configuration 传递给 Sinatra? - 2

    这是我的网络应用:classFront我是这样开始的(请不要建议使用Rack):Front.start!这是我的Puma配置对象,我不知道如何传递给它:require'puma/configuration'Puma::Configuration.new({log_requests:true,debug:true})说真的,怎么样? 最佳答案 配置与您运行的方式紧密相关puma服务器。运行的标准方式puma-pumaCLI命令。为了配置puma配置文件config/puma.rb或config/puma/.rb应该提供(参见examp

  5. ruby - 在 Ruby 中动态创建数组 - 2

    有没有办法在Ruby中动态创建数组?例如,假设我想遍历用户输入的书籍数组:books=gets.chomp用户输入:"TheGreatGatsby,CrimeandPunishment,Dracula,Fahrenheit451,PrideandPrejudice,SenseandSensibility,Slaughterhouse-Five,TheAdventuresofHuckleberryFinn"我把它变成一个数组:books_array=books.split(",")现在,对于用户输入的每一本书,我想用Ruby创建一个数组。伪代码来做到这一点:x=0books_array.

  6. jquery - 如何将 AJAX 变量从 jQuery 传递到他们的 Controller ? - 2

    我有一个电子邮件表格。但是我正在制作一个测试电子邮件表单,用户可以在其中添加一个唯一的电子邮件,并让电子邮件测试将其发送到该特定电子邮件。为了简单起见,我决定让测试电子邮件通过ajax执行,并将整个内容粘贴到另一个电子邮件表单中。我不知道如何将变量从我的HAML发送到我的Controllernew.html.haml-form_tagadmin_email_blast_pathdoSubject%br=text_field_tag'subject',:class=>"mass_email_subject"%brBody%br=text_area_tag'message','',:nam

  7. ruby - 是否可以将 IRB 提示配置为动态更改? - 2

    我想在IRB中浏览文件系统并让提示更改以反射(reflect)当前工作目录,但我不知道如何在每个命令后进行提示更新。最终,我想在日常工作中更多地使用IRB,让bash溜走。我在我的.irbrc中试过这个:require'fileutils'includeFileUtilsIRB.conf[:PROMPT][:CUSTOM]={:PROMPT_N=>"\e[1m:\e[m",:PROMPT_I=>"\e[1m#{pwd}>\e[m",:PROMPT_S=>"FOO",:PROMPT_C=>"\e[1m#{pwd}>\e[m",:RETURN=>""}IRB.conf[:PROMPT_MO

  8. ruby-on-rails - carrierwave:在序列化动态属性上安装 uploader - 2

    首先,我使用的是rails3.1.3和来自master的carrierwavegithub仓库的分支。我使用after_init钩子(Hook)来确定基于属性的字段页面模型实例并为这些字段定义属性访问器将值存储在序列化哈希中(希望它清楚我是什么谈论)。这是我正在做的事情的精简版:classPage省略mount_uploader命令让我可以访问我想要的属性。但是当我安装uploader时出现错误消息说“nil类的未定义新方法”我在源代码中读到有方法read_uploader和扩展模块中的write_uploader。我如何必须覆盖这些来制作mount_uploader命令使用我的“虚拟

  9. ruby - 如何将 lambda 传递给 Hash.each? - 2

    如何将lambda传递给hash.each,以便我可以重复使用一些代码?>h={a:'b'}>h.eachdo|key,value|end=>{:a=>"b"}>test=lambdado|key,value|puts"#{key}=#{value}"end>test.call('a','b')a=b>h.each&testArgumentError:wrongnumberofarguments(1for2)from(irb):1:in`blockinirb_binding'from(irb):5:in`each'from(irb):5from/Users/jstillwell/.rv

  10. ruby - 将命令行上的变量传递给 Cucumber 测试 - 2

    我正在尝试将cucumber项目的用户名和密码置于版本控制之外。有没有办法在命令行上手动将用户名和密码等变量传递给Cucumber脚本?我的备份计划是将它们放在一个YML文件中,然后将该文件添加到gitignore,这样它们就不会被置于版本控制中。 最佳答案 所以,我看到了您对铁皮人的评论,答案是肯定的。cucumberPASSWORD=my_passwordPASSWORD被设置为环境变量,您可以通过将其引用为ENV['PASSWORD']来使用它的值。例如,browser.text_field(:id=>'pwd').setEN

随机推荐