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 a controller (function) but the group will still be a Closure
Route::group(array('prefix' => 'admin', 'before' => 'auth'), function() // Closure
{
Route::get('/', 'Examplecontroller@bla'); // non Closure
});
Maybe for research purposes: Where are the routes cached?
Will grouped Laravel routes be cached? Where are they cached?
Yes, if body of the group is also another group or non-closure like route.
They are stored in bootstrap/cache
folder.
Closure like group (non cacheable):
Route::group(['middleware' => ['guest'], function() {
Route::get('/hi', function() {
dd('Hi I am closure');
});
});
Non-closure like group
Route::group(['middleware' => ['guest'], function() {
Route::get('/hi', 'WelcomeController@hi');
Route::get('/bye', 'WelcomeController@bye');
});
In fact second example is a closure (obviously) but (my guess is) Laravel will detect the closure contains only another routes (that are "cacheable") and rewrite it behind the scenes to following (this is not precisely correct and Laravel does not rewrite anything its simple demonstration how it might look, in reality Laravel uses Illuminate\Routing\RouteCollection
object):
Route::get('/hi', 'WelcomeController@hi')->middleware('guest');
Route::get('/bye', 'WelcomeController@bye')->middleware('guest');
And cache it.
My assumption is that Laravel does some kind of foreach + try/catch and if the body of group throws ErrorException (serialisation error) it simply aborts itself and shouts at coder that its not possible.
Code for $artisan route:cache
is here
And this is the code that determines if route is or is not "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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With