Kohana 3: using maintainable routes
I'm using Kohana v3 for a web project, and today I found myself writing this:
echo Html::an开发者_运维知识库chor('user/view/'.$user->id, "See user's profile");
If I rename the action_view
method in the User controller, the link will be broken. To avoid this, I created a function in the User model that returns the URL used for viewing the user's profile:
echo Html::anchor($user->url_view(), "See user's profile");
There is another (cleaner) way to do this (similar to Django's url()
and {% url %}
)?
PS: Excuse my English.
Yes you want reverse routing using the route name. Try something like this:
echo Html::anchor(
Route::get('your_route_name')->uri(array('id'=>$user->id)),
"See user's profile"
);
what do you need is called reverse routing. you have a route "name" (with parameters eventually) and you associate it with a module/action pair or with a whatever/path.
then you use this route name instead of the direct controller/action path so you can change the name of the actions as you like in future.
here's a page that describes this. be aware that in kohana 2.x reverse routing is not supported (so when he refers to 2.3 he really means 3.x)
精彩评论