Zend的路由反向匹配返回当前URL

时间:2011-12-01 16:45:13

标签: php zend-framework routing

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

这些是我设置的路线。

$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,我会收到以下错误:

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

将默认值添加到:category修复此问题的原因(建议欢迎)。 但这不是主要问题。

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

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

返回的值为http://example.com/categories。我完全难过了..

即使我尝试绕过默认路由器并且执行

,问题仍然存在
$router->removeDefaultRoutes();
$router->addRoute('default', new Zend_Controller_Router_Route(
    '*', 
    array('controller' => 'index', 'action' => 'index')
));

对Zend路由有更多经验的人是否想要解决此问题?

干杯, 乔恩

2 个答案:

答案 0 :(得分:4)

尝试更改此行

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

进入

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

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

<强> @Edit

经过深入研究后,我找到了另一种解决方案,如果你不想每次都传递路由器名称,你的代码应该像这样:

$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。

<?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

/类别

答案 1 :(得分:0)

为此,您必须在设置路由器时指定反向URL。与解释here一样。

我在我的项目中完成了这项工作,但是使用了INI文件配置。如下所示:

routes.myroute.type = "Zend_Controller_Router_Route_Regex"
routes.myroute.route = "devices/page/(\d+)"
routes.myroute.defaults.controller = "index"
routes.myroute.defaults.action = "devicesindex"
routes.myroute.map.1 = "page"
routes.myroute.reverse="devices/page/%d"

将其保存为配置文件夹中的INI文件或APP根目录中的某个位置。

在你的引导程序中,有类似的东西:

public static function setupCustomRoutes() {
    $router = self::$frontController->getRouter();
    $config = new Zend_Config_Ini(dirname(__FILE__) . '/config/routes.ini');
    $router->addConfig($config, 'routes');
}

使用以下代码在视图脚本中生成反向URL:

$this->url(array('page' => 3));

将生成

/devices/page/3/

我相信你可以修改你的代码,如下所示,使反向URL工作:

$router->addRoute('category', new Zend_Controller_Router_Route (
  'categories/:category',
  array(
    'controller' => 'category',
    'action' => 'modify',
  ),
  array('category' => 'category'),   //Not sure about this line. Do experiment.
  'categories/%d'
));
相关问题