如何编写ZF2路由器脚本以允许对默认操作进行参数 [英] How to write the ZF2 router script to allow parameters on the default action

查看:84
本文介绍了如何编写ZF2路由器脚本以允许对默认操作进行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绕过ZF2中路由声明的某些特质。 相册模块示例建议以下内容:

I am trying to get around some of the idiosyncrasies of the routing statements in ZF2. The Album module example suggests the following:

'router' => array(
    'routes' => array(
        'album' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/album[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Album\Controller\Album',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),

但是,让我们说,我想配置项目,以便URL / album / index / jazz 仅列出爵士流派的专辑。

Let’s say, however, that I wanted to configure the project so that the url, /album/index/jazz would list only the albums in the jazz genre.

我的第一个挑战是在 / album / index / jazz 中,第二个参数不是 [/: id] 。它想成为 [/:genre] 。而且,虽然我可以轻松地将路由更改为 route => ‘/ album [/:action] [/:arg1]’‘arg1’=>的约束; '[a-zA-Z] [a-zA-Z0-9 _-] *',我真的不想放弃'[0-9] +',对ID的约束。我要寻找的是一种根据动作是定义其他参数模式和约束的方法。

My first challenge is that in /album/index/jazz the second argument is not [/:id]. It wants to be [/:genre]. And, while I could easily just change the route to 'route' => '/album[/:action][/:arg1]', and the constraint to 'arg1' => '[a-zA-Z][a-zA-Z0-9_-]*', I don’t really want to give up the '[0-9]+', constraint on the id. What I’m looking for is a way to define alternative parameter patterns and constraints depending upon what the action is.

我的第二个挑战在于ZF2对默认动作的处理。如果我将路由设置如下:

My second challenge lies in ZF2’s treatment of the default action. If I set up the routing as follows:

'router' => array(
    'routes' => array(
        'album' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/album[/:action][/:arg1]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'arg1'   => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),
                'defaults' => array(
                    'controller' => 'Album\Controller\Album',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),

然后,URL / album / index / jazz 会将 jazz参数发送给控制器,如我所愿。但是,让ZF2使用默认操作构造URL时遇到问题。遵循相册模块示例中 index.phtml 的语法,我的期望是:

then the url /album/index/jazz will send the "jazz" argument to the controller as I want it to. However, I have a problem getting ZF2 to construct the url using the default action. Following the syntax from the index.phtml in the Album module example, my expectation is that the following:

    <a href="<?php echo $this->url('album',
        array('action'=>'index', 'agr1' => 'jazz'));?>">Jazz</a>

将构造< a href = / album / index / jazz >爵士< / a> 。但是(我假设因为索引是默认操作),索引被剥夺,ZF2提供< a href = / album / jazz> Jazz< / a> ,并非以实际网页为目标。我尝试将 index 重命名为路由器,控制器和视图中的其他内容;但发现只要是默认操作, $ this-> url(…)函数就会删除我使用的任何名称。解决此问题的方法可能有很多,但是我认为最好在路由器配置中解决它,以免引起框架其他未知部分的惊讶,这些未知部分可能会类似地处理默认操作。

would construct <a href="/album/index/jazz "> Jazz </a>. But (I assume because index is the default action) index gets stripped out and ZF2 gives <a href="/album/jazz ">Jazz</a>, which doesn’t target a real page. I’ve tried renaming index to something different in the router, the controller and the view; but have found that as long as it’s the default action, the $this->url( … ) function will strip out whatever name I use. There are probably a number of ways around this issue, but I think it’s best to solve it in the router configuration to avoid surprises with other unknown parts of the framework that may treat the default action similarly.

推荐答案

我找到了一种方法来解决以下问题:默认参数被排除在 url()助手进行配置。当zf2通过route语句开发url时,它将丢弃与默认参数匹配的 [] 括号内的所有内容。因此,

I have found a way to work around the issue that the default parameters get left out of the url that the url() helper configures. When zf2 develops the url from the route statements, it drops out everything within the [] brackets that match the default parameters. Therefore,

    <a href="<?php echo $this->url('album',
        array('action'=>'index', 'arg1' => 'jazz'));?>">Jazz</a>

构造< a href = / album / jazz>爵士< ; / a> 代替< a href = / album / index / jazz> Jazz< / a> 包括'route'=> ‘/ album [/:action] [/:arg1] ,就像我在问题中提到的那样。但是,如果我从参数中断开 / 字符并赋予它自己的 [] 括号, url()助手仅删除URL的:action 部分。换句话说,如果我替换

constructs <a href="/album/jazz">Jazz</a> instead <a href="/album/index/jazz">Jazz</a> of when the route statement includes 'route' => '/album[/:action][/:arg1]', as I presented in the question. However, if I breakout the / character from the parameter(s) and give it it's own[] brackets, the url() helper only drops the :action part of the url. In other words, if I replace

                'route'    => '/album[/:action][/:arg1]',

with

                'route'    => '/album[/][:action][/][:arg1]',

然后

    <a href="<?php echo $this->url('album',
        array('action'=>'index', 'arg1' => 'jazz'));?>">Jazz</a>

构造< a href = / album // jazz> Jazz< / a> ,指向它应指向的页面。

constructs <a href="/album//jazz ">Jazz</a>, which points to the page it should.

我也从 timdev 的答案中得到了很多,退后一步并决定自己想要的方式是值得的您的网址方案起作用,然后弄清楚如何实现它。如果我想对查询,过滤和排序数据添加很多麻烦, index 操作(管理多个专辑)将想要采用与 add edit 删除完全不同的参数集操作(每个操作一次只能管理一张专辑。)因此,我设置了两条路线:

I also got a lot out of timdev's answer, "It's worth it to take a step back and decide how you want your url scheme to work, and then figure out how to implement it." If I want to add a whole lot of bells and whistles to how I want to query, filter and sort the data, theindex action (which manages multiple albums) is going to want to take a completely different set of parameters than the add, edit and delete actions (which each only manage a single album at a time.) So I set up two routes:

'router' => array(
    'routes' => array(
        'albums' => array(
            ...
                'route'    => '/albums[/][:action] [/][:arg1] [/][:arg2] [/][:arg3]   ...  ',
            ...
        ),
        'album' => array(
            ...
                'route'    => '/album[/][:action][/][:id]',
            ...
        ),
    ),
),

这篇关于如何编写ZF2路由器脚本以允许对默认操作进行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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