Which Laravel Helper Do You Use for Your URLs?
Published on by Eric L. Barnes
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
action('UserController@profile', ['id' => 1])// 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
route('user.profile', ['id' => 1])// 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
url('user/profile', [1])// 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
href="/user/profile/{{ $user->id }}"// 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:
public function getUriAttribute($_){ return $this->created_at->format('Y/m/').$this->slug.'/';}
Then, in your Blade file:
<a href="{{ $post->uri }}">{{ $post->title }}</a>
Now, you can easily change the post’s permalink across your whole site.
Conclusion
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.
Eric is the creator of Laravel News and has been covering Laravel since 2012.