As we all know caching helps site to load faster . When a new visitor comes to your site a saved or cached page is served instead sending bulk request to a server ! you can learn more here about caching https://en.wikipedia.org/wiki/Cache_%28computing%29 . so today you will learn abouut Route caching in laravel 5.2 . which helps to speed things up. In Laravel 5.2 , a caching mechanism was introduced to speed up the execution

Lets take a simple example of routes.php

Route::post('reserve-room', '[email protected]');

Route::controllers([
  'auth' => 'Auth\AuthController',
  'password' => 'Auth\PasswordController',
]);
Route::post('/bookRoom','[email protected]', ['middleware' => 'auth', 'domain'=>'booking.hotelwebsite.com']);

Route::resource('rooms', 'RoomsController');

Route::group(['middleware' => ['auth','whitelist']], function()
{

  Route::resource('accommodations', 'AccommodationsController');
  Route::resource('accommodations.amenities', 'AccommodationsAmenitiesController');
  Route::resource('accommodations.rooms', 'AccommodationsRoomsController');
  Route::resource('accommodations.locations', 'AccommodationsLocationsController');
  Route::resource('amenities', 'AmenitiesController');
  Route::resource('locations', 'LocationsController');
});

By running the following command, Laravel will cache the routes:

$ php artisan route:cache

Then, place them into the following directory:

/vendor/routes.php

Here is a small portion of the resultant file:

<?php

/*

| Load The Cached Routes
|--------------------------------------------------------------------------
|
| Here we will decode and unserialize the RouteCollection instance that
| holds all of the route information for an application. This allows
| us to instantaneously load the entire route map into the router.
|
*/

app('router')->setRoutes(
  unserialize(base64_decode('TzozNDoiSWxsdW1pbmF0ZVxSb3V0aW5nXF JvdXRlQ29sbGVjdGlvbiI6NDp7czo5OiIAKgByb3V0ZXMiO2E6Njp7czozOiJH RVQiO2E6NTA6e3M6MToiLyI7TzoyNDoiSWxsdW1pbmF0ZVxSb3V0aW5nXFJvdX RlIjo3OntzOjY6IgAqAHVyaSI7czoxOiIvIjtzOjEwOiIAKgBtZXRob2RzIjth OjI6e2k6MDtzOjM6IkdFVCI7aToxO3M6NDoiSEVBRCI7fX
...
Db250cm9sbGVyc1xBbWVuaXRpZXNDb250cm9sbGVyQHVwZGF0ZSI7cjoxNDQx O3M6NTQ6Ik15Q29tcGFueVxIyb2xsZXJzXEhvdGVsQ29udHJvbGxlckBkZXN0c m95IjtyOjE2MzI7fX0='))
);

As the DocBlock states, the routes are encoded in base64 and then serialized:

unserialize(base64_decode( … ));

This performs some precompilation. If we base64 decode the contents of the file, we obtain the serialized data. The following code is an extract of the file:

O:34:"Illuminate\Routing\RouteCollection":4:{s:9:"*routes"; a:6:{s:3:"GET";a:50:{s:1:"/";O:24:"Illuminate\Routing\Route": 7:{s:6:"*uri";s:1:"/";s:10:"*methods";a:2:{i:0;s:3:"GET";i:1; s:4:"HEAD";}s:9:"*action";a:5:{s:4:"uses";s:50:"MyCompany \Http\Controllers\[email protected]";s:10:"controller"; s:50:"MyCompany\Http\Controllers\[email protected]"; s:9:"namespace";s:26:"MyCompany\Http\Controllers";s:6:"prefix"; N;s:5:"where";a:0:{}}s:11:"*defaults";a:0:{}s:9:"*wheres"; a:0:{}s:13:"*parameters";N;s:17:"*parameterNames";N; }s:4:"home";O:24:"Illumin…

"MyCompany\Http\Controllers\[email protected]";r:1632;}}

If the /vendor/routes.php file exists, it is used instead of the routes.php file that is located at /app/Http/routes.php. If at some point using the route caching file is no longer desired, use the following artisan command:

$ php artisan route:clear

This command will delete the cached routes file and Laravel will begin using the /app/Http/routes.php file again.

Tip

It is important to note that if there are any closures used in the routes.php file, then caching will fail. Here is an example of a closure in a route:

Route::get('room/{$id}', function(){
  return Room::find($id);
});

It is inadvisable to use closure in the routes.php file for any reason. To be able to use route caching, relocate the code used within the closure into a controller.

Advertisements