开发者

CakePHP - Html->link - why use controller=> and action=> instead of just controller/action

Why this:

echo $this->Html->link('Add a User', array('cont开发者_JAVA技巧roller'=>'users', 'action'=>'add'));

Instead of just this:

echo $this->Html->link('Add a User', 'users/add');


The second example will always generate a url of 'users/add'. The first provides the potential of using reverse routing to form a bespoke url, as defined by the rules in your routes.php file.

In practice I often find that there's no difference between the first and the second style. However, if you later decide to make changes to your routes, you might find that doing things the first time saves time in the long run, so you don't have to go back and change the path for every link...


For future reference... use the first one, 'cos the second one is relative to the location. For example if you are in www.example.com/post the final url whil be:

for #1 www.example.com/post/users/add for #2 www.example.com/users/add

Sorry for the bad english :P

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜