CakePHP 3-路由问题


CakePHP 3 - Routing issue

我目前正在将一个v2站点迁移到v3,遇到了一个路由问题,我自己无法解决,所以这里可能有人也遇到了同样的问题。所以我有这个路线:

http://example.com/installer-review/postcodes/...

路线规则是:

$routes->connect('/installer-review/postcodes/*',
    ['controller' => 'InstallerReviews', 'action' => 'postcodes'],
);

当我转到url时,它会正确地重定向到InstallerReviews的操作"邮政编码",但无论何时我都会尝试:路由器::url([‘controller’=>‘installer review’,‘action’=>‘postcode’]);要生成指向InstallerReviews操作的链接,请执行以下操作:http://example.com/installer_review/postcodes

V2没有发生这种情况,现在V3也发生了这种情况。

我在路由文件中使用了不同的classRoutes,但没有起到任何作用,我一直在使用InflectedRoute来增加与以前版本的兼容性,但在使用InfectedRoute调试路由生成时,它会将所有"-"转换为"_",这打破了我原来的路由规则。

因此,由于这个问题,我尝试创建一个自定义路由规则,并遵循[http://book.cakephp.org/3.0/en/development/routing.html#custom-路线类别][1],但这是另一个令人头疼的问题,如果我这样做的话:

$routes->connect('/installer-review/postcodes/*',
    ['controller' => 'InstallerReviews', 'action' => 'postcodes'],
    ['routeClass' => 'MyRoute']
);

它开始抛出关于已经声明的类MyRoute的错误。

我现在的解决方案是实现我自己的Url::build函数,它覆盖了原始的helper函数,但这对我来说太"黑客"了,所以我希望有一个更干净的解决方案。

很抱歉有这么大的墙,但不想遗漏任何内容,感谢您的帮助

路由器::url([‘controller’=>‘installer review’,‘action’=>‘postcode’]);

这是不正确的。在3.x中,您必须使用'controller'的类名,因此它应该是Router::url(['controller' => 'InstallerReviews', 'action' => 'postcodes']);