I dislike the Html->link() method. I appreciate it, but it uglies up the code and I don’t believe that something so basic should require a method. However, in my project, I find I must use it if I want to have proper URLs. I’ll accept that, but I want to know that I have to before continuing making links in this (large) project.
I’ve attempted to find some ways to get the cakeURL in a view. Nothing worked out.
My goal is to go from this:
<?=$this->Html->link('quality view', array('controller' => 'quals', 'action' => 'show')); ?>
To this:
<a href="<?=URL.'quals/show'?>">
But I cannot write or find a way to get such a constant working if I change the page’s URL. (even something such as visiting /quals/ and /quals will show different URLs)
I also dislike the link method, I find it overkill for adding attributes like class, id and target=”_blank” to my links.
I do this for links:
So, I still use the HTML helper to get the URL, but the html element I code myself. I would recommend doing it that way, rather than hard-coding the URLs. This is the Cake way to do it, and it allows you to take full advantage of Cake’s inbuilt routing functionality in routes.php, and create pretty routes without having to hard-code or remember them in more than one place.
It also makes it easier for others looking at your code in future – eg. does the hard-coded ‘quals/show’ link refer to the quals/show directory in your webroot? Or does it refer to the show action of the quals controller? (and it only gets more complex when you start working with plugins). If you use the html helper to create URL’s, all that stuff is immediately clear.
If you want to make it look tidy, you could break it into two lines like this:
If you’ve got a large project, it’s even more reason to use Cake’s helpers for URLs. It may seem like a good idea now, but hard coding them will give you a big headache at some point in the future.