Many cakephp newbies might not know how to correctly generate a url for their app, for maybe a link or a redirect etc, especially if your cake app is in a subdirectory.
So let’s say you have a website www.example.com, and you have a blog written with cakePHP in a sub directory www.example.com/blog.
it is very easy to just make a link like this:
<a href="/blog/post/view/2">link to post</a>
Of course you can’t just use ‘post/view/2’ as the link href, because you would then end up at www.example.com/post/view/2 or any other weird directory which is not what we want.
So the best way for cake to generate the correct url that we want every time would be to use:
< ?php App::import('Router'); echo '<a href="'.Router::url(array('controller' => 'post', 'action' => 'view', 2)).'>Link to post</a>'; ?>
With the Router class and its function ‘url’, cake would always create the correct url for all your links.
And another great thing about it is that you can move your app around to any directory or sub directory or sub domain and all your links would still work without a problem.
Of course it would be best to use
<?php echo $html->link('Link to post', array('controller' => 'post', 'action' => 'view', 2)); ?>
in your views, but you can use Router::url() anywhere in your app, if for some reason you would want to create a link outside of a view.