Php Zend反向匹配路由返回当前URL
我正在使用Zend框架,并尝试设置一些自定义路由。 在分派过程中,这些功能似乎运行良好,但反向匹配根本不起作用 这些是我设定的路线Php Zend反向匹配路由返回当前URL,php,zend-framework,routing,Php,Zend Framework,Routing,我正在使用Zend框架,并尝试设置一些自定义路由。 在分派过程中,这些功能似乎运行良好,但反向匹配根本不起作用 这些是我设定的路线 $router->addRoute('category', new Zend_Controller_Router_Route( 'categories/:category', array( 'controller' => 'category', 'action' => 'modify', )
$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'
)
));
如果现在转到,将执行正确的控制器和操作(category.index)。如果转到,则会出现以下错误:
Fatal error: Uncaught exception 'Zend_Controller_Router_Exception' with
message 'category is not specified'
将默认值添加到:category修复了此问题(欢迎建议)。
但这不是主要问题
每当我的应用程序执行$this->url(…)
时,返回的值始终是当前url。所以,如果我跑
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路由有更多的经验,想尝试一下吗
干杯,
Jon因此,在设置路由器时,必须指定反向URL。就像我解释的那样 我已经在我的项目中完成了这项工作,但使用的是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文件或应用程序根目录中的某个位置
在您的引导程序中,有如下内容:
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'
));
试着换一行
echo $this->url(array('controller' => 'category', 'action' => 'modify', 'category' => 5))
进入
第二个参数是url函数应该使用的路由名称。如果未传递,则帮助器将使用当前路由
@编辑
经过深入研究,我找到了解决您问题的另一种方法,如果您不想每次传递路由器名称,您的代码应该如下所示:
$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帮助器
<?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/>
输出应为:
/类别/5
/类别你能试着切换这两条路线吗?切换路线变化不大。URL映射到标准Zend模式。我可以直接访问/categories并获得正确的页面,但是/categories/2会产生“categority is not specified”。。。编辑:当调用$this->url而不指定'category'=>…,即使我试图链接到其他控制器/操作时,也不会指定类别。是的,我知道我可以手动指定路由,但我更希望有一个解决方案,不必在任何地方都手动指定该类别,以获得url()如果“类别”是当前正在应用的路线,则它将起作用。选中此项了解当前路由-但我并不总是只链接到当前路由?因此您需要传递路由名称,选中Zend/View/Helper/Url.php,您将看到如果不传递路由名称,Zend将采用当前路由。当然,您可以编写自己的查看url帮助程序。但我不想每次调用url()时都传递路由名称。这就是路线反向匹配的全部意义…嗯。。根据Zend手册(),这应该只适用于Regex路由器?“由于正则表达式模式不容易反转,如果您希望使用URL助手或此类的汇编方法,则需要准备反转URL。”不,这不适用于Route_Route。我得到:“可捕获的致命错误:传递给Zend_控制器_路由器_路由的参数4::_构造()必须是Zend_Translate的实例,给定字符串”
<?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/>