Will Laravel routes be grouped? Where are they cached?

As stated in the docs

Closure-based routes cannot be cached. To use route caching, you must convert any Closure routes to controller classes.

But if I want to group routes, I can make the route itself point to the controller (function), but the group will still be Closure

Route::group(array('prefix' => 'admin', 'before' => 'auth'), function() // Closure
{
    Route::get('/', 'Examplecontroller@bla'); // non Closure
});

      

Perhaps for research purposes: where are the routes cached?

+3


source to share


2 answers


Will Laravel's grouped routes be cached? Where are they cached?

Yes, as long as the group body is also another group or non-closure like a route.

They are stored in a folder bootstrap/cache

.


Behind the scenes

Closing as a group (not cached ):

Route::group(['middleware' => ['guest'], function() {
    Route::get('/hi', function() {
        dd('Hi I am closure');
    });
});

      

Group without snapping

Route::group(['middleware' => ['guest'], function() {
    Route::get('/hi', 'WelcomeController@hi');
    Route::get('/bye', 'WelcomeController@bye');
});

      



Actually the second example is closure (obviously) but ( my guess ). Laravel detects that the closure only contains other routes (which are "cacheable") and rewrites them behind the scenes as follows (this is not entirely correct, and Laravel does not rewrite anything simple to show how it might look, Laravel actually uses an object Illuminate\Routing\RouteCollection

):

Route::get('/hi', 'WelcomeController@hi')->middleware('guest');
Route::get('/bye', 'WelcomeController@bye')->middleware('guest');

      

And cache it.

My guess is that Laravel is doing some kind of foreach + try / catch and if the group body throws an ErrorException (serialization error) it just interrupts itself and yells at the coder that this is not possible.


Code for $artisan route:cache

here

And this is the code that determines if the route is "cacheable" from route.php

public function prepareForSerialization()
{
    if ($this->action['uses'] instanceof Closure) {
        throw new LogicException("Unable to prepare route [{$this->uri}] for serialization. Uses Closure.");
    }

    $this->compileRoute();

    unset($this->router, $this->container);
}

      

+3


source


Bootstrap Directory

The directory bootstrap

contains files that load the framework and configure autoloading. This directory also contains a directory cache

containing files generated by frames for performance optimization, such as route and service cache files.



Documentation

+1


source







All Articles