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 .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/** * Get the current Url using the URL * facade * * http://laravel-urls.local/foo */ Route::get('/foo', function () { return URL::current(); }); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/** * Get the current Url using the url * Service Container Binding * * http://laravel-urls.local/foo */ Route::get('/bar', function () { return url()->current(); }); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/** * Get the full URL of the current page including the query string * parameters * * http://laravel-urls.local/url/full?page=2 */ Route::get('url/full', function () { return URL::full(); }); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** * Click on this link to go to the url/previous Route * */ Route::get('click/link', function () { return '<a href="/url/previous">Click me</a>'; }); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/** * Get the previous Url * * http://laravel-urls.local/click/link */ Route::get('url/previous', function () { return URL::previous(); }); |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/** * Generate an absolute url to the homepage * * http://laravel-urls.local */ Route::get('url/to', function () { return URL::to('/'); }); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/** * Generate an absolute url to the homepage * and pass parameters * * http://laravel-urls.local/1/bar */ Route::get('url/to/params', function () { return URL::to('/', ['id' => 1, 'foo' => 'bar']); }); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/** * Generate a secure absolute URl to the homepage * * https://laravel-urls.local/1/bar */ Route::get('url/to/secure', function () { return URL::to('/', ['id' => 1, 'foo' => 'bar'], true); }); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/** * A named route. * */ Route::get('url/named', [ 'as' => 'named_route', 'uses' => function () { return 'named route'; } ]); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/** * Generate a url with a parameter id of 1 to the named * route * * http://laravel-urls.local/url/named?id=1 */ Route::get('url/route', function () { return URL::route('named_route', ['id' => 1]); }); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/** * Check if a URL is valid or not * */ Route::get('url/validate', function () { if (URL::isValidUrl('http://laravel-urls.local/url/named')) { return "valid url"; } return 'invalid url'; }); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//Generate a secure url to a route <a href="{{ secure_url('/foo') }}">Secure Url</a> //Generate a url to a route with parameters <a href="{{ url('/url', ['id'=> 'full']) }}">Url with parameters</a> //Generate a url to a named route <a href="{{ route('named_route') }}">Named route</a> //Generate A url to an asset <a href="{{ asset('img/logo.png') }}">Asset Url</a> //Generate A url to an asset <a href="{{ asset('css/style.css') }}">Secure asset Url</a> |
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.
- Laravel Url Generation - April 8, 2016