16

In Django, I can do this:

<a href="{% url 'account_login' %}">Account Link</a>

which would give me domain/account/login where I have that URL named in my urls.py

url(r'^account/login/$', views.Login.as_view(), name='account_login'),

I want to do something similar in Laravel 5.2

I currently have something like this:

Route::get('/survey/new', ['as' => 'new.survey', 'uses' => 'SurveyController@new_survey']);

How do I use in my template, plus passing in parameters?

I came across this: https://laravel.com/docs/5.1/helpers, but it was just a piece of a white page without relevant content of how to actually use it.

KhoPhi
  • 9,660
  • 17
  • 77
  • 128

1 Answers1

55

You can use the route() helper, documented here.

<a href="{{ route('new.survey') }}">My Link</a>

If you include laravelcollective/html you could use link_to_route(). This was originally part of the core but removed in Laravel 5. It's explained here

{!! link_to_route('new.survey', 'My Link') !!}

The Laravel Collective have documented the aforementioned helper here. The function prototype is as follows

link_to_route($routeName, $title = null, $parameters = [], $attributes = []);

If for example you wanted to use parameters, it accepts an array of key value pairs which correspond to the named segments in your route URI.

For example, if you had a route

Route::get('surveys/{id}', 'SurveyController@details')->name('detail.survey');

You can generate a link to this route using the following in the parameters.

['id' => $id]

A full example, echoing markup containing an anchor to the named route.

{!! link_to_route('new.survey', 'My Link', ['id' => $id]) !!}
Ben Swinburne
  • 25,669
  • 10
  • 69
  • 108
  • You know, what you wrote ain't helping. You just posted exactly what is on the docs site. My question is, in my template with a url like this: `My Link` to get the link generated for me. All what you posted above doesn't do that, does it? – KhoPhi Aug 08 '16 at 20:46
  • You didn't explicitly ask for how to generate markup; and without knowledge of django `url` would suggest you generate a url to the route, not markup. I will edit my answer. – Ben Swinburne Aug 08 '16 at 20:48
  • I ended the question by saying: `How do I use in my template, plus passing in parameters?` - How will I be able to use in template without it generating a markup, unless that's possible in Laravel. – KhoPhi Aug 08 '16 at 20:52
  • 1
    You can use a URL in a template just as you can use a URL within an anchor's href attribute in a template. There's a difference between a URL and markup containing the URL; both of which can be output in a template. Nevertheless, I've updated my answer. Hope it's helpful. – Ben Swinburne Aug 08 '16 at 20:55
  • So, if I want to use the `link_to_route()` with params, it'll be something like this? `{{ link_to_route('detail.survey', 'The Title', [$survey->id]) }}` <- this gives me error. Will appreciate your help regarding appending a param. The docs don't mean anything to me since the docs don't make any sense. – KhoPhi Aug 08 '16 at 21:04
  • 1
    What error do you get? You'll need to use `{!! !!}` to prevent the markup being double escaped if you use `link_to_route()`. See [Other Control Structures: Displaying Raw Text With Curly Braces](https://laravel.com/docs/5.0/templates#other-blade-control-structures). If I remember rightly the params are key value pairs. I.e. if your route is like `surveys/{id}` the parameters would be `['id' => $survey->id]` – Ben Swinburne Aug 08 '16 at 21:08
  • Got it, Thanks! You may perhaps wanna add the params example to your answer above. Thanks for the clarification. Useful! Shame on Laravel docs – KhoPhi Aug 08 '16 at 21:12
  • No worries. I've updated the answer with the clarification. – Ben Swinburne Aug 08 '16 at 21:26