tags:

views:

415

answers:

2

I'm using Kohana v3 for a web project, and today I found myself writing this:

echo Html::anchor('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.

A: 

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)

gpilotino
You're correct about reverse routing, but that linked example is outdated. The best resources are the official userguide: http://v3.kohanaphp.com/guide/ and the unofficial wiki: http://kerkness.ca/wiki/doku.php
rick
+6  A: 

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"
);
rick
Thanks a lot, I didn't know about the Route class.
dusan