Which Laravel Helper Do You Use for Your URLs?


April 6th, 2017

Which Laravel Helper Do You Use for Your URLs?

Jacob Bennett did a Twitter poll to see what the community uses for referencing URLs in their Laravel applications. With 500 votes, the overwhelming majority (77%) said they use the route() helper:

Curious…when referencing URLs in your @laravelphp views, which do you use?

RTs welcome! Thanks! ❤️

— Jacob Bennett (@JacobBennett) April 3, 2017

This was a complete surprise to me as I typically only use the URL string style, but it did make me go back to the documentation and take a look at all the different styles that could be employed in a project. Let’s take a look at some of the advantages and disadvantages of each style.

Laravel Action Helper

1action('UserController@profile', ['id' => 1])
2// http://site.dev/user/profile/1

Advantages: The result is tied to your controller and method name, so you do not have to name each route individually.

Disadvantages: It’s subjective, but the biggest issue I have with this style is you have to type a lot of characters for all your links. The second—and larger—disadvantage is if you happen to change your controller or method names you’ll have to hunt down all the uses each time.

Laravel Route Helper

1route('user.profile', ['id' => 1])
2// http://site.dev/user/profile/1

Advantages: This style is tied to a named route; if you change a controller name or controller method all your links will continue to work.

Disadvantages: You have to name each route.

Laravel URL Helper

1url('user/profile', [1])
2// http://site.dev/user/profile/1

Advantages: Like the previous two, the URL helper automatically prepends the full domain name that you have set in config/app.php. The advantage to this option over the others is it is simpler. You do not have to define route names or even controllers.

Disadvantages: If you change your routes you will need to update each use of the string manually.

URL Strings

1href="/user/profile/{{ $user->id }}"
2// href="/user/profile/1"

Advantages: This shares the same benefit as the URL helper. It’s simple, easy to follow, and as close as possible to standard HTML.

Disadvantages: This does not append the site’s domain name. So you need to be sure you know where the app will be running and that it’s never going to be in a subdirectory.

Other URL Solutions

Outside of the common solutions mentioned above, I have sometimes used the model to generate the URLs. For example, imagine you are building a blog, and you want the URL for the post details to be in the “yyyy/mm/slug” format. One trick is to take advantage of the model accessor feature:

1public function getUriAttribute($_)
3 return $this->created_at->format('Y/m/').$this->slug.'/';

Then, in your Blade file:

1<a href="{{ $post->uri }}">{{ $post->title }}</a>

Now, you can easily change the post’s permalink across your whole site.


With all of these different styles, which one is right for your project? I’m afraid there isn’t a best practice, and the answer is going to come down to you, your team, the size of the app, etc. There are so many variables in place that a one-size-fits-all solution is not possible. What I can say, is I’ve been happy using the string style on small to medium size apps with one to two developers.

Once you start moving to a bigger team and a larger app, I’d imagine either the action or route will be a better solution. The important thing is to decide on one and use it across the project, instead of mixing and matching.

Filed in:

Eric L. Barnes

Eric is the creator of Laravel News and has been covering Laravel since 2012.