Fluent Routing in Laravel 5.2
This is a series of posts that showcase new features and aha moments in Laravel 5.2
1. Implicit Route Model Binding
2. Simplified Eloquent Global Scopes
3. Append Scheduled Tasks Output
9. Fluent Routing
Introduction
Laravel 5.2 has come bundled with a lot more options of doing things and one of those beautiful options is Fluent routing.
Now, this Fluent Routing option has been available since Laravel 5.1.17.
I am only reiterating it now in Laravel 5.2 because a lot of developers are really not aware of it yet.
Fluent routing is the ability to chain several methods used in routing to give a very nice and fluent interface.
Example
In Laravel 5.1 < Laravel 5.1.17, we can write a route like so:
1 2 3 4 5 6 |
Route::get('/songs/{songs}', [ 'uses' => 'SongController@index', 'as' => 'singleSong', 'middleware' => 'api', 'prefix' => '/api/v2' ]); |
Here this route uses the SongController
, invokes the api
middleware, has a prefix of /api/v2
and has a named route of singleSong
.
We had to pass all those options into an array.
In Laravel 5.1.17 <= Laravel 5.2, we can simply just chain them as methods anyhow we like it like so:
1 2 |
Route::get('/songs/{songs}', 'SongController@index') ->prefix('/api/v1')->middleware('api')->name('singleSong'); |
From the code above, we have the following methods:
1. prefix => for appending prefixes to your routes
2. middleware => for applying middleware, here you can also pass in an array of several middlewares.
3. name => for named routes
Try it out and see the awesome fluent blessing for yourself.
Let me show you the code that makes this possible.
Go into your vendor/laravel/framework/src/Illuminate/Routing/Route.php
From line 623 to 636:
prefix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/** * Add a prefix to the route URI. * * @param string $prefix * @return $this */ public function prefix($prefix) { $uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/'); $this->uri = trim($uri, '/'); return $this; } |
From 761 to 772:
name
1 2 3 4 5 6 7 8 9 10 11 12 |
/** * Add or change the route name. * * @param string $name * @return $this */ public function name($name) { $this->action['as'] = isset($this->action['as']) ? $this->action['as'].$name : $name; return $this; } |
From line 229 to 250:
middleware
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/** * Get or set the middlewares attached to the route. * * @param array|string|null $middleware * @return array */ public function middleware($middleware = null) { if (is_null($middleware)) { return (array) Arr::get($this->action, 'middleware', []); } if (is_string($middleware)) { $middleware = [$middleware]; } $this->action['middleware'] = array_merge( (array) Arr::get($this->action, 'middleware', []), $middleware ); return $this; } |
return $this
in each of those methods is what gives it the ability to be chained to other methods.
Conclusion
There are lots of hidden treasures in large sets of codebase. Take time to explore Laravel Codebase, every day you’ll learn something new and become better at writing and organizing good code.
Note: The version of Laravel used here is 5.2.0
Please, let me know if you have any questions or observations in the comments section below

- How to build your own Youtube – Part 10 - August 1, 2016
- How to build your own Youtube – Part 9 - July 25, 2016
- How to build your own Youtube – Part 8 - July 23, 2016
- How to build your own Youtube – Part 6 - July 6, 2016
- Introducing Laravel Password v1.0 - July 3, 2016
- How to build your own Youtube – Part 5 - June 28, 2016
- How to build your own Youtube – Part 4 - June 23, 2016
- How to build your own Youtube – Part 3 - June 15, 2016
- How to build your own Youtube – Part 2 - June 8, 2016
- How to build your own Youtube – Part 1 - June 1, 2016