路由的Zend反向匹配返回当前URL [英] Zend reverse matching of routes returns current URL

查看:78
本文介绍了路由的Zend反向匹配返回当前URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Zend Framework,并且正在尝试设置一些自定义路线。
这些似乎在分派期间工作正常,但是反向匹配根本不起作用。

I'm using Zend Framework, and am trying to set up some custom routes. These seem to be working fine during dispatch, but the reverse matching isn't working at all.

这些是我设置的路线。

$router->addRoute('category', new Zend_Controller_Router_Route(
    'categories/:category',
    array(
        'controller' => 'category',
        'action' => 'modify',
    )
));

$router->addRoute('categories', new Zend_Controller_Router_Route_Static(
    'categories',
    array(
        'controller' => 'category',
        'action' => 'index'
    )
));

如果我现在去 http://example.com/categories ,将执行正确的控制器和操作(category.index)。如果我转到 http://example.com/categories/5 ,则会出现以下错误:

If I now go to http://example.com/categories, the correct controller and action are executed (category.index). If I go to http://example.com/categories/5, I get the following error:

Fatal error: Uncaught exception 'Zend_Controller_Router_Exception' with 
message 'category is not specified'

将默认值添加到:category可以解决此问题,这是有原因的(建议)。
但这不是主要问题。

Adding a default value to :category fixes this problem for reason (suggestions welcome). That is not the major problem though.

每当我的应用程序执行 $ this-> url(...),返回的值始终是当前URL。因此,在 http://example.com/categories 上,如果我运行

Whenever my application executes $this->url(...), the returned value is always the current URL. So, on http://example.com/categories, if I run

echo $this->url(
    array('controller' => 'category', 'action' => 'modify', 'category' => 5)
);

返回值为 http://example.com/categories 。我完全感到困惑。

The returned value is http://example.com/categories. I am completely stumped..

即使我尝试绕过默认路由器并执行此操作,问题仍然存在

The problem persists even if I try to bypass the default router and do

$router->removeDefaultRoutes();
$router->addRoute('default', new Zend_Controller_Router_Route(
    '*', 
    array('controller' => 'index', 'action' => 'index')
));

在Zend路由方面有更多经验的人是否想对此有所了解?

Does anyone with a bit more experience with Zend routing want to have a crack at this?

干杯,
Jon

Cheers, Jon

推荐答案

尝试更改此行

echo $this->url(array('controller' => 'category', 'action' => 'modify', 'category' => 5))

进入

echo $this->url(array('controller' => 'category', 'action' => 'modify', 'category' => 5), 'category')

第二个参数是url函数应使用的路由名称。如果未通过,则帮助程序将使用当前路线。

the second parameter is route name that url function should use. If its not passed helper will user current route.

@Edit

经过更深入的研究,我发现了另一个解决您问题的方法,如果您不想每次都通过路由器名称,则您的代码应如下所示:

After deeper research I have found another solution for your problem, if you dont want to pass each time router name your code should looke like this:

$router->addRoute('category', new Zend_Controller_Router_Route(
    'categories/:id',
    array(
        'controller' => 'category',
        'action' => 'modify',
        'id' => 0
    ),
    array(
      'id' => '\d+'
    )
 ));

$router->addRoute('categories', new Zend_Controller_Router_Route_Static(
    'categories',
    array(
        'controller' => 'categories',
        'action' => 'index'
    )
)); 

这将设置路由器部分,现在调用url helper。

This will be setting the router part, now call url helper in view.

<?php echo $this->url(array('controller' => 'category', 'action' => 'modify', 'id' => 5), null, true);?><br/>
<?php echo $this->url(array('controller' => 'category', 'action' => 'index'), null, true);?><br/>

输出应为:

/ Categories / 5

/categories/5

/ categories

/categories

这篇关于路由的Zend反向匹配返回当前URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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