Route caching

Laravel 5 has a new mechanism for caching the routes as the routes.php file can easily grow very large and will quickly slow down the request process. To enable the caching mechanism, type the following artisan command:

$ php artisan route:cache

This creates another routes.php file in /storage/framework/routes.php. If this file exists, then it is used instead of the routes.php file, which is located in app/Http/routes.php. The structure of the file is as follows:

<?php

/*
|--------------------------------------------------------------------------
| Load The Cached Routes
|
…
*/

app('router')->setRoutes(
unserialize(base64_decode('TzozNDoiSWxsdW1pbmF0ZVxSb3V0aW5nXFJvdXRlQ29sbGVjdGlvbiI6NDp7czo5OiIAKgByb3V0ZXMiO2E6Njp7czozOiJHRVQiO2E6M
…
... VyQGluZGV4IjtzOjk6Im5hbWVzcGFjZSI7czoyNjoiTXlDb21wYWbXBhbnlcSHR0cFxDb250cm9sbGVyc1xIb3RlbENvbnRyb2xsZXJAZGVzdHJveSI7cjo4Mzg7fX0='))
);

Notice that an interesting technique is used here. The routes are serialized, then base64 is encoded. Obviously, to read the routes, the reverse is used, base64_decode(), and then unserialize().

If the routes.php cached file exists, then every time a change is made to the routes.php file, the route cache artisan command must be executed. This will clear the file and then recreate it. If you later decide to no longer use this mechanism, then the following artisan command can be used to eliminate the file:

$ php artisan route:clear

Laravel is useful for building several distinctly different types of applications. When building traditional web applications, there is often a tight integration between the controllers and the views. It is also useful when building an app that can be used on a smartphone. In this case, the frontend will be created for the smartphone's operating system using another programming language and/or framework. In this case, only the controllers and model will most likely be used. In either case, however, having a well-documented RESTful API is an essential part of a well-designed modern software.

Nested controllers helps developers right away to read the code—it is an easy way to understand that the particular controller deals with the "nesting" or the concept that one class is related another.

Type-hinting the models and objects into the controller also improves the readability and, at the same time, reduces the amount of code necessary to perform the basic operations on the objects.

Also, eloquent model casting creates an easy way to transform the attributes of a model, without having to rely on external packages or tedious accessor functions, as was the case in Laravel 4.

Now it is rather clear to us why Laravel is becoming the choice of many developers. Learning and repeating some of the steps illustrated in this chapter will allow a RESTful API to get created in under an hour for a small-to-medium size program.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset