Laravel 4 - Blade Templating - How to properly Link to Route?

54,119

Solution 1

Use URL::route() to get just a link:

<a href="{{ URL::route('user/profile/', $params) }}">
     <i class="icon-dashboard"></i>
     <span class="menu-text"> Dashboard </span>
</a>

Solution 2

If you Route use a Closure, you can use URL::to(), like this

<a href="{{ URL::to('home/otherpage', $params) }}">
    <i class="icon-dashboard"></i>
    <span class="menu-text"> Dashboard </span>
</a>

As @orrd sugested, in general terms is better to use named routes, so it can be easily change the URL later:

<a href="{{ URL::route('routeName', $params) }}">
    <i class="icon-dashboard"></i>
    <span class="menu-text"> Dashboard </span>
</a>

(ref: https://laravel.com/docs/5.0/helpers#urls)

Solution 3

if you define Route name you can use that in your blade :

 Route::get('/admin/transfer/forms-list', [
    'as'   => 'transfer.formsList',
    'uses' => 'Website\TransferController@IndexTransferForms'
]);

now you can use that in your blade like this :

<a href="{{URL::route('transfer.formsList')}}"  type="submit">
                    discard</a>

Solution 4

There are no of ways to use route in blade:

1. Use Action

{{URL::action('DemoController@index',$params)}}

2. Use Route

{{ URL::route('route/', $params) }}

3. Use URL to

{{ URL::to('route/name', $params)) }}
Share:
54,119
LoveAndHappiness
Author by

LoveAndHappiness

Updated on January 28, 2020

Comments

  • LoveAndHappiness
    LoveAndHappiness over 4 years

    I want to create a resourceful link with Laravel. Normally I just use the {{ link_to_route('Yadayadayada.route', 'LinkName', $params }}

    But in this case I am using a Template with this layout:

    <a href="index.html">
         <i class="icon-dashboard"></i>
         <span class="menu-text"> Dashboard </span>
    </a>
    

    That means that inside the anchor tag, is as well a <i>-Tag and a <span>-Tag. Is it possible to use the {{ link_to_route }}-Method, without having to change the layout of the Template?