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.
click here
You’re so awesome! I don’t suppose I’ve truly read through a single thing like that before. So good to discover somebody with unique thoughts on this subject. Seriously.. thanks for starting this up. This web site is something that is needed on the internet, someone with a little originality!
toy uav drone
It’s a pity you don’t have a donate button! I’d certainly donate
to this outstanding blog! I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account.
I look forward to new updates and will share this site with my
Facebook group. Chat soon!