29

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?

Jatin
  • 3,065
  • 6
  • 28
  • 42
LoveAndHappiness
  • 9,735
  • 21
  • 72
  • 106

6 Answers6

46

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>
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • Thank you very much for an astoundingly quick answer. – LoveAndHappiness Mar 31 '14 at 01:00
  • How can I make the same technique for destroy function?? I tried href="{{ route('shops.destroy', $row->id ) }}" data-method="delete" but I redirects me to show() instead!!!! – Dr. MAF Jan 20 '15 at 17:16
  • 1
    @Dr.MAF - `href="{{ route('shops.destroy', $row->id).'?_method=delete' }}"` – Joseph Silber Jan 20 '15 at 18:26
  • @Dr.MAF you must include the javascript file `rails.js` from the https://github.com/rails/jquery-ujs project (if you want to maintain it from laravel, use https://github.com/efficiently/jquery-laravel ). Then use what you wrote in your comment. – Pascal Hurni Jan 23 '15 at 10:41
17

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)

Alejandro Silva
  • 8,808
  • 1
  • 35
  • 29
  • 1
    It's better to use named routes whenever possible rather than repeating the URL multiple places in your code (so you can easily change the URL later in one place if you needed to). – orrd Dec 20 '16 at 19:32
  • 1
    @orrd thats right, i've updated the response with you sugestion – Alejandro Silva Dec 21 '16 at 20:42
5

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>
Hamid Naghipour
  • 3,465
  • 2
  • 26
  • 55
5

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) }}
shasi kanth
  • 6,987
  • 24
  • 106
  • 158
Govind Samrow
  • 9,981
  • 13
  • 53
  • 90
0

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>
Papa Kent
  • 25
  • 7
0

{{ link_to_route('dashboard', 'Dashboard', '', array('class'=>'body')) }}

Sam Prasanna
  • 131
  • 1
  • 3