我正在为用户控制器路由.该控制器有大约20种方法,所以我不想在路由中手动设置它们:
I'm having a route for user controller. This controller has about 20 methods so I don't want to set them manually in routes:
Route::set('user', 'user/<action>') ->defaults(array( 'controller' => 'user', 'action' => 'index', ));我还有图片控制器(Controller_User_Pictures),它具有用于管理用户图片的多种方法.当我创建路线时:
I have also pictures controller (Controller_User_Pictures) with multiple methods that is used to manage users pictures. When I create a route:
Route::set('pictures', 'user/pictures/<action>') ->defaults(array( 'directory' => 'user', 'controller' => 'user_pictures', 'action' => 'index', ));它不起作用.
即使我创建了一个单独的控制器(Controller_Pictures)并创建了路由,它也不起作用:
Even if I create a separate controller (Controller_Pictures) and create route it's not working:
Route::set('pictures', 'user/pictures/<action>') ->defaults(array( 'directory' => 'user', 'controller' => 'pictures', 'action' => 'index', ));在所有情况下,它都会返回错误:Method action_pictures does not exist,这意味着路由器正在用户控制器中寻找称为图片的操作,这当然是错误的.
In all cases it returns error: Method action_pictures does not exist which means that router is looking for an action called pictures in user controller which of course is wrong.
我唯一想到的解决方案是在bootstrap.php文件中定义所有路由.您有更好的解决方案吗?
The only solution that I'm thinking of, is to define all routes in bootstrap.php file. Do you have better solution?
修改 第二个问题,是否知道如何在任何操作中(当然是在路由中)用连字符替换下划线?
Edit Second question, do have any idea how to replace underscore with hyphen in any actions (of course in routing)?
推荐答案路由的顺序很重要;具体路线应该优先.
The order of the routes is important; specific routes should come first.
因此您的网址user/pictures/<action>正在触发第一条路由.如果将图片路线放置在常规用户路线上方,则应该可以正常工作.
So your url user/pictures/<action> is triggering the first route. If you put the pictures-route above the general user-route, it should work just fine.
另一注:如果您指定目录用户",则您的控制器将在classes/controller/user/pictures.php中的Controller_User_Pictures
Another side-note: if you specify directory 'user', your controller would be Controller_User_Pictures in classes/controller/user/pictures.php
更多推荐
KohanaPHP 3路由问题
发布评论