简体   繁体   English

Kohana 3:使用可维护的路线

[英]Kohana 3: using maintainable routes

I'm using Kohana v3 for a web project, and today I found myself writing this: 我正在将Kohana v3用于一个Web项目,今天我发现自己正在编写以下代码:

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. 如果我在用户控制器中重命名action_view方法,则链接将断开。 To avoid this, I created a function in the User model that returns the URL used for viewing the user's profile: 为了避免这种情况,我在User模型中创建了一个函数,该函数返回用于查看用户个人资料的URL:

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 %} )? 还有另一种(更干净的)方法可以做到这一点(类似于Django的url(){% url %} )?

PS: Excuse my English. PS:对不起,我的英语。

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) 请注意,在kohana 2.x中不支持反向路由(因此当他提到2.3时,他的意思是3.x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM