Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

route:cache with duplicate routes throws wrong exception #7452

Closed
minorgod opened this issue Feb 15, 2015 · 2 comments
Closed

route:cache with duplicate routes throws wrong exception #7452

minorgod opened this issue Feb 15, 2015 · 2 comments

Comments

@minorgod
Copy link

I stumbled upon this weird behavior when testing route caching. If I have a routes file containing only the following routes:

Route::get('home', 'WelcomeController@index');
Route::get('home', 'HomeController@index');

Running artisan route:cache throws exception:
exception 'Exception' with message 'Serialization of 'Closure' is not allowed' in ..\vendor\laravel\framework\src\Illuminate\Foundation\Console\RouteCacheCommand.php:95

After carefully inspecting the RouteCollection passed to the buildRouteCacheFile() function in the RouteCacheCommand.php, I see no indication that there are any closures in the collection.

I realize it's silly to define the 'home' route twice pointing to different controllers, but it seems like the wrong exception is being thrown in the above case.

@jobrios
Copy link

jobrios commented Feb 16, 2015

RouteCollection maintains 4 arrays of routes: $routes, $allRoutes, $nameList, and $actionList. After routes #1 and #2 are created for the 'home' path, route #2 overwrites route #1 in $routes and $allRoutes. But $actionList, which indexes Route instances by the controller-method "action" string, still holds route #1 ($nameList remains empty since you don't have named routes in this case). This is because $actionList is the only array for which the two routes have different indices ('App\Http\Controllers\WelcomeController@index' != 'App\Http\Controllers\HomeController@index').

In RouteCacheCommand::fire, the foreach loop iterates over the routes in $allRoutes, so it misses preparing route #1 (which is still in $actionList) for serialization. If, after the loop, you run

$routes->getByAction('App\Http\Controllers\WelcomeController@index')->prepareForSerialization();

you won't get the exception.

The line unset($this->container) in Route::prepareForSerialization stops the attempt at serializing the application which is responsible for the exception.

@jobrios
Copy link

jobrios commented Feb 16, 2015

Also, just saw that PR #7432 addresses this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants