对非必需的Kohana Route :: url使用默认路由参数

时间:2021-05-29 19:15:30

I have a route setup that looks like this:

我有一个如下所示的路由设置:

Route::set('my_route', 'r/<controller>(/<action>)(/(<name>-)<hash>)', array(
    'controller'    => '[a-z]+',
    'action'        => '[a-z]+',
    'hash'          => '\w{13}',
    'name'          => '[a-z]+',
))->defaults(array(
    'directory'     => 'my_dir',
    'controller'    => 'welcome',
    'action'        => 'index',
    'name'          => null,
));

Which works by itself. The problem comes in when I try and build a URL from the route, like this:

哪个有效。当我尝试从路由构建URL时出现问题,如下所示:

return Route::url('my_route', array(
    'action'    => 'test',
)); 

I get this error message:

我收到此错误消息:

Kohana_Exception [ 0 ]: Required route parameter not passed: name

So if I set name to null, I get the same result. If I set name to false, there is no error message, but the urls look like this:

因此,如果我将name设置为null,我会得到相同的结果。如果我将name设置为false,则没有错误消息,但是url看起来像这样:

/r/welcome/test/-

/ R /首页/测试/ -

notice the - on the end? Now I could strip that off, but I'm hoping there’s a better way.

注意 - 最后?现在我可以解除它,但我希望有更好的方法。


1 个解决方案

#1


1  

It seems to me that you are trying to fix your routing by using 1 route for all. This is not the right way to do things.

在我看来,你正试图通过使用1路由来修复你的路由。这不是正确的做事方式。

Just make multiple routes (maybe you have to tweak this):

只需制作多条路线(也许你必须调整一下):

Route::set('my_route', 'r/<controller>(/<action>)/(<name>-)<hash>', array(
    'controller'    => '[a-z]+',
    'action'        => '[a-z]+',
    'hash'          => '\w{13}',
    'name'          => '[a-z]+',
))->defaults(array(
    'directory'     => 'my_dir',
    'controller'    => 'welcome',
    'action'        => 'index',
    'name'          => null,
));

Route::set('my_route2', 'r/<controller>(/<action>)', array(
    'controller'    => '[a-z]+',
    'action'        => '[a-z]+',
    'hash'          => '\w{13}',
    'name'          => '[a-z]+',
))->defaults(array(
    'directory'     => 'my_dir',
    'controller'    => 'welcome',
    'action'        => 'index',
    'name'          => null,
));

Remember the route system is really powerful and more routes doesn't mean it gets slower. So just make as many clearly possible routes and don't try to run everything by 1 route.

请记住,路线系统非常强大,更多路线并不意味着它变慢。因此,只需制作尽可能多的明确可能的路线,不要试图通过1路线运行所有路线。

#1


1  

It seems to me that you are trying to fix your routing by using 1 route for all. This is not the right way to do things.

在我看来,你正试图通过使用1路由来修复你的路由。这不是正确的做事方式。

Just make multiple routes (maybe you have to tweak this):

只需制作多条路线(也许你必须调整一下):

Route::set('my_route', 'r/<controller>(/<action>)/(<name>-)<hash>', array(
    'controller'    => '[a-z]+',
    'action'        => '[a-z]+',
    'hash'          => '\w{13}',
    'name'          => '[a-z]+',
))->defaults(array(
    'directory'     => 'my_dir',
    'controller'    => 'welcome',
    'action'        => 'index',
    'name'          => null,
));

Route::set('my_route2', 'r/<controller>(/<action>)', array(
    'controller'    => '[a-z]+',
    'action'        => '[a-z]+',
    'hash'          => '\w{13}',
    'name'          => '[a-z]+',
))->defaults(array(
    'directory'     => 'my_dir',
    'controller'    => 'welcome',
    'action'        => 'index',
    'name'          => null,
));

Remember the route system is really powerful and more routes doesn't mean it gets slower. So just make as many clearly possible routes and don't try to run everything by 1 route.

请记住,路线系统非常强大,更多路线并不意味着它变慢。因此,只需制作尽可能多的明确可能的路线,不要试图通过1路线运行所有路线。