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
$ 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
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 Turorial will allow a RESTful API to get created in under an hour for a small-to-medium size program.
A RESTful API provides an easy way to expand the program in the future and also integrates with third-party programs and software that exist within a company that might need to communicate with the application. The RESTful API is the front-most shell of the inner part of the program and provides the bridge between the outside world and the application itself. The inner part of the program will be where all of the business logic and database connections will reside, so fundamentally, the controllers simply have the job of connecting the routes to the application.
Laravel follows the RESTful best practices, thus documenting the API should be easy enough for other developers and third-party integrators to understand. Laravel 5 has brought a few features in to the framework to enable the code to be more readable.
In future articles, middleware will be discussed. Middleware adds various “middle” layers between the route and the controller. Middleware can provide features such as authentication. Middleware will enrich, protect, and help organize the routes into logical and functional groups.
We will also discuss DocBlock annotations. Annotations, while not natively supported in PHP, can be enabled via a Laravel community package. Then, inside the DocBlock of the controller and controller functions, the routing for each controller is automatically created, without having to actually modify the
app/Http/routes.php file. This is another great community concept that Laravel easily adapts to, in the same manner as phpspec and Behat.