CakePHP路由与正则表达式 [英] CakePHP route with regex
问题描述
我有一个控制器设置接受两个变量: / clients / view / var1 / var2
I have a controller setup to accept two vars: /clients/view/var1/var2
我想显示为 / var1 / var2
我试过
Router::connect('/*', array('admin'=>false, 'controller' => 'clients', 'action' => 'view'));
但这会停止所有其他控制器作为 / *
路线一切
But this stops all other controllers working as /*
routes everything
网站上的所有其他网页均位于 admin
前缀所以基本上我需要一个被忽略的路由,如果当前前缀 admin
!我试过这个(regex来自正则表达式匹配字符串不包含一个字?):
All other pages that are on the site are within the admin
prefix so basically i need a route that is ignored if the current prefix is admin
! I tried this (regex is from Regular expression to match string not containing a word?):
Router::connect('/:one', array('admin'=>false, 'controller' => 'clients', 'action' => 'view'), array(
'one' => '^((?!admin).*)$'
));
它要求测试控制器,而不是客户端
But I think the regex is incorrect because if i naviate to /test
it asks for the tests controller, not clients
我唯一的其他路线是:
Router::connect('/admin', array('admin'=>true, 'controller' => 'clients', 'action' => 'index'));
Router::connect('/', array('admin'=>false, 'controller' => 'users', 'action' => 'login'));
我做错了什么?谢谢。
推荐答案
我第一次误解了您的问题。我测试你的代码,也没有得到预期的结果。原因可能是正则表达式解析器不支持否定lookahead断言。但我仍然认为你可以通过重新排列路线来解决这个问题:
I misunderstood your question the first time. I tested your code and didn't get the expected result either. The reason might be that the regex parser doesn't support negative lookahead assertion. But I still think you can solve this with reordering the routes:
CakeBook描述了如果使用前缀路由则自动生成哪些路由。在你的情况下,这些路由必须在'/ *'路由之前手动分配以捕获所有管理操作。这里是为我工作的代码:
The CakeBook describes which routes are automatically generated if you use prefix routing. In your case these routes have to be assigned manually before the '/*'-route to catch all admin actions. Here is the code that worked for me:
// the previously defined routes
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/admin', array('controller' => 'clients', 'action' => 'index', 'admin' => true));
// prefix routing default routes with admin prefix
Router::connect("/admin/:controller", array('action' => 'index', 'prefix' => 'admin', 'admin' => true));
Router::connect("/admin/:controller/:action/*", array('prefix' => 'admin', 'admin' => true));
// the 'handle all the rest' route, without regex
Router::connect(
'/*',
array('admin'=>false, 'controller' => 'clients', 'action' => 'view'),
array()
);
现在我得到所有我的管理控制器操作与管理员前缀和/ test1 / test2被重定向到客户端控制器。
Now I get all my admin controller actions with the admin prefix and /test1/test2 gets redirected to the client controller.
这篇关于CakePHP路由与正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!