Generating a HTML Link to a Controller Action

Problem

You want to generate a link to a specific controller action.

Solution

Use the HTML::linkAction() method.

Note that the controller and action you specify must exist AND there must be a reference to it somewhere in your app/routes.php file.

The simplest form is a single argument, the controller@action.

{{ HTML::linkAction('Home@index') }}

This could produce something like.

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

You can specify the title as the second argument.

{{ HTML::linkAction('Home@index', 'Home') }}

Which produces the following.

<a href="http://your.url/index">Home</a>

If the controller action method takes arguments, you can specify them in the third parameter, as a simple array.

{{ HTML::linkAction('ItemController@show', 'Show Item #3', array(3)) }}

The HTML would look like below (depending on your routes).

<a href="http://your.url/items/3">Show Item #3</a>

And, if additional attributes are needed use the fourth parameter.

{{ HTML::linkAction('Home@index', 'Home', array(), array('class' => 'btn')) }}

Now the anchor has a class attribute.

<a href="http://your.url/index" class="btn">Home</a>

Discussion

Nothing to discuss.

comments powered by Disqus