带有默认子目录的 Kohana 3.1 路由
我有一个应用程序,在我尝试实现用于登录和注册的身份验证模块之前,该应用程序基本上可以正常工作。
我的应用程序目录结构基本上是:
application
-- classes
-- controller
-- admin
(admin area)
-- block
(blocks to display within pages)
-- page
(default pages)
默认情况下,我想要 URL,例如 http://www.testsite.com/test 它将访问 Controller_Page_Test 类。或者显式调用管理或阻止页面 http://www.testsite.com/admin/test它将访问 Controller_Admin_Test 类。更复杂的是,它还需要处理可选的操作和 ID。
我在顶部说过,这基本上工作正常 - 直到我尝试添加 Auth 模块。 Auth 模块调用 http://www.testsite.com/user/login 但不是访问模块的默认路径,它在页面目录中查找。
为了克服这个问题,我放置了一个更高级别的路由,但现在它已成为我的默认页面处理程序。显式调用仍然可以通过。
我的路线目前如下所示:
Route::set('user', '(<controller>(/<action>(/<id>)))', array('controller' => 'user|admin_user'))
->defaults(array(
'controller' => 'user',
'action' => 'index',
'id' => NULL,
));
Route::set('with_dir', '<directory>/<controller>(/<action>(/<id>))', array('directory' => 'block|admin'))
->defaults(array(
'directory' => 'page',
'controller' => 'home',
'action' => 'index',
));
Route::set('just_id', '<controller>(/<id>)', array('id' => '\d+'))
->defaults(array(
'directory' => 'page',
'controller' => 'home',
'action' => 'index',
));
Route::set('auto_dir', '<controller>(/<action>(/<id>))', array('id' => '\d+'))
->defaults(array(
'directory' => 'page',
'controller' => 'home',
'action' => 'index',
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'prototype',
'action' => 'index',
));
可以更好地清理吗?如何让该模块仅在我需要时启动?
I have an application that is essentially working until I tried to implement an Auth module for logins and registrations.
My application directory structure is basically:
application
-- classes
-- controller
-- admin
(admin area)
-- block
(blocks to display within pages)
-- page
(default pages)
By default I want to have URL's such as http://www.testsite.com/test which would access the Controller_Page_Test class. Or to explicitly call admin or block pages http://www.testsite.com/admin/test which would access the Controller_Admin_Test class. To further complicate matters it also needs to handle optional actions and id's.
I said at the top that this is basically working correctly - until I've tried to add in the Auth module. The Auth module calls http://www.testsite.com/user/login but instead of accessing the module's path via the default, it looks in the page directory.
To overcome this I placed a higher level route but now this has become my default page handler. Explicit calls still get through.
My routes currently look like this:
Route::set('user', '(<controller>(/<action>(/<id>)))', array('controller' => 'user|admin_user'))
->defaults(array(
'controller' => 'user',
'action' => 'index',
'id' => NULL,
));
Route::set('with_dir', '<directory>/<controller>(/<action>(/<id>))', array('directory' => 'block|admin'))
->defaults(array(
'directory' => 'page',
'controller' => 'home',
'action' => 'index',
));
Route::set('just_id', '<controller>(/<id>)', array('id' => '\d+'))
->defaults(array(
'directory' => 'page',
'controller' => 'home',
'action' => 'index',
));
Route::set('auto_dir', '<controller>(/<action>(/<id>))', array('id' => '\d+'))
->defaults(array(
'directory' => 'page',
'controller' => 'home',
'action' => 'index',
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'prototype',
'action' => 'index',
));
Can this be cleaned up any better? And how do I get this module to only kick in when I need it to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,可以更好地清洁。 Kohana 开发人员鼓励使用此框架的人们根据需要添加尽可能多的路由。您甚至可以为每个操作指定它们,这将使您将来能够更改 URL(例如,您可能希望使用
/signin
而不是/user/login
),如果您使用正确的方法来生成链接等(例如Route::url()
helper)。现在,这里是指定
user
路由的另一种方法:它将仅匹配请求,其中 URI 的第一部分已给出并且等于
user
代码>或<代码>admin_user。以前,控制器部分是可选的,因此也匹配对/
URI 的调用。Yes, it can be cleaned better. Kohana developers encourage people using this framework to add as much routes as needed. You can even specify them for each action which will enable you to change URLs in the future (eg. instead of
/user/login
you may wish to have/signin
), if you use proper methods to generate links etc. (eg.Route::url()
helper).Now, saying that, here is the other way to specify the
user
route:Which will match only the requests, where the first part of the URI is given and is equal either to
user
oradmin_user
. Previously the controller part was optional, thus was also matching calls to/
URI.