开发者

Should I use array or string in link param in CakePHP HtmlHelper?

CakePHP HtmlHelper link() method accepts 2 types of variable as second parameter (the link URL param).

Now I wonder if using array for the param, like

array('controller'=>'users','action'=>'login')

is slower than using string, like '/users/login'. Because Helper won't have to parse the array, just display the link.

If it is so, then what is the purpose of link开发者_开发技巧() method? For now, I am using HtmlHelper::url() method with a regular <a> to display all of my links, to keep my template clean!

Please correct me :)


One reason is reverse routing: For examples, if you route '/blah' to array('controller'=>'articles','action'=>'index'). When you create the link with array('controller'=>'articles','action'=>'index'), cake can automatically output '/blah'. It may not sound very interesting; but if later on you change the route to '/foo', then the link() method can automatically change the output to '/foo'.

Another reason is: using array you can build the url in a programmatic fashion. It's not just controller and action, you also have prefix, named parameters, your own custom parameters if you create in routes, etc.

For now, I am using HtmlHelper::url() method with a regular <a> to display all of my links, to keep my template clean! Well, you are making it harder on yourself then :)


This provides a consistent and flexible method for creating hyperlinks, referencing controllers / actions and specifying dynamic options via associative arrays. Performance shouldn't be an issue unless you are iteratively creating links. Even then, array management and implosion is usually much more efficient than string concatenation.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜