Take the 2-minute tour ×

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)

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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:

<a class="myclass" href="<?php echo $this->Html->url(array('controller'=>'my_controller','action'=>'my_action','plugin'=>false)); ?>">My Anchor Text</a>

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:

<a class="myclass" href="<?php echo $url; ?>" >My Anchor Text</a>

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.

share|improve this answer
As stated by Joshua, the advantage of HtmlHelper::link() is that Cake will automatically change all your links if you decide to add to routes.php. It may be cumbersome just to add attributes to your links, but know that it is a framework that is attempting to structure everything. If you really want to just use the vanilla <a> element, you can use the Cake global constant FULL_BASE_URL. So you would use <a href="<?php echo FULL_BASE_URL; ?>/quals/show">CLICK HERE</a>. –  penguin egg Jan 31 '13 at 5:53
I'm confident in accepting this answer. url() is lighter and better for my needs. Your second example is perfect for what I want to do. The first is great too. Penguin, thanks. I think I'll stick with url() for now. As the project's grown in size, I find my ../ just isn't cutting it anymore, especially with fancy AJAX. :} Cheers. –  Vael Victus Feb 1 '13 at 5:28
add comment

There are actually very serious considerations for using HtmlHelper::link()!

When you're using the HtmlHelper for building links:

  • You have one central plase to define all routes in the application. The URL structure will always be the same as the one defined in Config/routes.php. When you change something there all links in the site will automatically reflect these changes
  • Reverse Routing
  • Easy application maintenance
  • SEO/Sitemapping

So if you use it you gain the abbility to change the structure of your application with minor changes, otherwise if you change one link you'd have to go through all the places it is used and change it manually.

As @joshua.paling said hardcoding the URLs is not a good idea since you'll have a lot of headaches with any change to the structure. Best is to use HtmlHelper's link() and/or url() methods.

share|improve this answer
add comment

Your Answer


