Laravel Url Generation

When designing a new site, there are a few qualities that every user visiting your site will notice. Some can be argued out, like clear call to action across the site, while others just need to be easily noticeable.

You have probably guessed by now that I am talking about user friendly navigation. A visitor to your site needs a GPS that guides them on how to move from one page to the next or simply, how to download a file. You can always trust me not to go back to your website if I have to manually type urls to get what I need or I simply see links I do not need.

But how do we achieve this? Again, you guessed it right,  Hyperlinks!

Hyperlinks, or simply put, links allow easy navigation based on URLs(Uniform resource Locators). Laravel makes it all easy to construct your URLs for your site so that you can concentrate on what’s important. Your site and your users. In this post, we will go through a few of the methods in the URL Facade that you can take advantage of.

The Current Url

The URL facade extends the Illuminate\Routing\UrlGenerator class. With the beauty of Laravel’s service container binding, we can all access it through the url() method .


The full Url

We now have the current url but something is missing. What happens when your url has some query string parameters passed to it? For instance, in the example about, visit /foo?page=2.

Weird right? Laravel totally ignores the query string parameters when you use url()->current(). We can however use URL::full() to get the complete url.

One way you could take advantage of this is when you want to display something on the homepage (say, a landing page) but not on the subsequent paginated pages like I did here.

The Previous Url

To get the previous Url request as is set by the HTTP Request headers Referer, use the previous method.


Generate url

The to method is used to generate an absolute URL to the given path.

It takes a string parameter to the route, optional array parameters and optional boolean secure parameter



The to method, however, does not validate whether the Url exists or not. Be careful not to generate broken links.

Other common Url generator methods include.

  • Secure – Generate a secure, absolute URL to the given path.
  • Asset – Generate a URL to an application asset.
  • assetFrom – Generate a URL to an asset from a custom root domain such as CDN.

1. secureAsset() – Generate a URL to a secure asset.

You can also generate a url to a named route using the route method. The upside of using route is that the Url is automatically updated.

Validate url

The final method that we will look at is the isValidUrl which returns a boolean value on whether the passed Url is valid or not.

Url Generator helper methods

Laravel provides some helper methods that are easy to use and handy in blade templates that keep the views short and neat. The most commonly used helper methods are as follows.

There are a few more methods that you might want to tinker with on the official Laravel API documentation here.

To get the code or contribute to this blog repository, Here is the link to the repo for used for this project.

Please if you have any questions or observations, let me know in the comments section.

John Kariuki

About John Kariuki

John Kariuki is a software developer at Andela, an avid blog reader and a lover of everything tech. He has experience in PHP/Laravel, MEAN(MongoDB, Express.js, AngularJS, Node.js) and Python stacks. ​ John holds a bachelor's degree in Information and Technology from Jomo Kenyatta University of Agriculture and Technology. ​ He is an avid basketball fan and is currently learning Russian. In his free time, he loves to play basketball, swim and jog.