Skip to content

Instantly share code, notes, and snippets.

@zrhoffman
Last active July 18, 2019 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zrhoffman/91a5d9a0f03c75781fe4933409537240 to your computer and use it in GitHub Desktop.
Save zrhoffman/91a5d9a0f03c75781fe4933409537240 to your computer and use it in GitHub Desktop.
Laravel route collision problem
<?php
use Illuminate\Support\Facades\Route;
/*
* Routes:
* second/fifth
* third/sixth
*
* ./artisan route:list
* +--------+----------+----------------------------+------+---------+--------------+
* | Domain | Method | URI | Name | Action | Middleware |
* +--------+----------+----------------------------+------+---------+--------------+
* | | GET|HEAD | api/user | | Closure | api,auth:api |
* | | GET|HEAD | {group}/{page} | | Closure | web |
* | | GET|HEAD | {group}/{page}/{optional?} | | Closure | web |
* +--------+----------+----------------------------+------+---------+--------------+
*/
Route::prefix('{group}')->where(['group' => 'first'])->group(function () {
Route::get('{page}', function () {
return 'This page will 404.';
})->where(['page' => 'fourth']);
});
Route::prefix('{group}')->where(['group' => 'second'])->group(function () {
Route::get('{page}', function (string $group, string $page) {
return "This is the {$page} page in the {$group} group.<br>In a larger project, this route would go to Controller2.";
})->where(['page' => 'fifth']);
});
Route::prefix('{group}')->where(['group' => 'third'])->group(function () {
Route::get('{page}/{optional?}', function (string $group, string $page) {
return "This is the {$page} page in the {$group} group.<br>In a larger project, this route would go to Controller3.";
})->where(['page' => 'sixth', 'optional' => 'A string no one will guess']);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment