Generating a HTML Link to a Named Route

Problem

You want to generate a link to one of your routes.

Solution

Use the HTML::linkRoute() method.

The only required argument is the first one, the name of the route.

{{ HTML::linkRoute('login') }}

Depending on your app/routes.php file, this may output something like.

<a href="http://your.url/user/login">http://your.url/user/login</a>

If you don't have a route with the name specified, an error will get generated.

You can pass a second argument to specify the title to display.

{{ HTML::linkRoute('login', 'Sign In') }}

This produces something similar to the following (based on routes.php).

<a href="http://your.url/user/login">Sign In</a>

If you're route takes parameters, then you must pass a third argument.

{{ HTML::linkRoute('items.show', 'Show item #4', array(4)) }}

The output could look something like below.

<a href="http://your.url/items/4">Show item #4</a>

You can specify an array as the fourth parameter. This array should contain any additional attributes to apply to the anchor tag.

{{ HTML::linkRoute('login', 'Sign In', array(), array('class' => 'btn')) }}

Now the anchor tag has a class attribute.

<a href="http://your.url/user/login" class="btn">Sign In</a>

Discussion

Nothing to discuss.

comments powered by Disqus