Php 可选路线停止具有相同模式的其他路线
下面是我定义可选变量的路径,我将有一个中间件来处理可选变量。我不知道为什么这会使测试页面无法访问,我相信它会被解释为invite代码。解决这个问题的方法是什么Php 可选路线停止具有相同模式的其他路线,php,laravel,laravel-5,Php,Laravel,Laravel 5,下面是我定义可选变量的路径,我将有一个中间件来处理可选变量。我不知道为什么这会使测试页面无法访问,我相信它会被解释为invite代码。解决这个问题的方法是什么 Route::group(['middleware' => ['friend']], function() { Route::get('/{invite_code?}', 'IndexController@index')->name('home')->middleware('firsttime'); // 首页
Route::group(['middleware' => ['friend']], function() {
Route::get('/{invite_code?}', 'IndexController@index')->name('home')->middleware('firsttime'); // 首页
Route::get('/testing/{invite_code?}', 'IndexController@testing')->name('testing');
});
例如,我有这三条路线
Route::get('/{invite_code?}', function () {
dd('i am invite code Route');
})->name('invitecode');
Route::get('/myCustomRoute', function () {
dd( 'i am myCustomRoute');
});
Route::get('/testing/{invite_code?}', function(){
dd( 'i am testing invite code Route');
})->name('invitecodetest');
现在我在8000端口为我的应用程序提供服务,所以http://localhost:8000/
是我的应用程序url
现在当我点击myCustomRoute
时,它实际上不会点击myCustomRoute
,这是因为路由器是myCustomRoute
作为invite\u code
点击http://localhost:8000/myCustomRoute
因此结果将是我是邀请代码路线
Route::get('/{invite_code?}', function () {
dd('i am invite code Route');
})->name('invitecode');
Route::get('/myCustomRoute', function () {
dd( 'i am myCustomRoute');
});
Route::get('/testing/{invite_code?}', function(){
dd( 'i am testing invite code Route');
})->name('invitecodetest');
如何解决这个问题
有两种方法
路线1重新排列路线
Route::get('/myCustomRoute', function () {
dd( 'i am myCustomRoute');
});
Route::get('/testing/{invite_code?}', function(){
dd( 'i am testing invite code Route');
})->name('invitecodetest');
Route::get('/{invite_code?}', function () {
dd('i am invite code Route');
})->name('invitecode');
现在再次尝试点击web.php末尾的add{invite\u code?}
路径
点击http://localhost:8000/myCustomRoute
结果将是我是我的客户路线
Route::get('/{invite_code?}', function () {
dd('i am invite code Route');
})->name('invitecode');
Route::get('/myCustomRoute', function () {
dd( 'i am myCustomRoute');
});
Route::get('/testing/{invite_code?}', function(){
dd( 'i am testing invite code Route');
})->name('invitecodetest');
现在它工作正常,但有一个缺点
现在试试http://localhost:8000/myundefinedroutename
但是它会击中{invite\u code?}
路径,所以这是一个bug
方法2尝试为路由添加一些前缀
Route::get('/invidecode/{invite_code?}', function ( $invite_code = null) {
dd( $invite_code, "i am invite code Route");
})->name('invitecode');
Route::get('/myCustomRoute', function () {
dd( 'i am myCustomRoute');
});
Route::get('/testing/{invite_code?}', function(){
dd( 'i am testing invite code Route');
})->name('invitecodetest');
现在试试http://localhost:8000/invidecode/yourcodegoeshere
结果将是
“YourCodeGoesher”
“我是邀请代码路线”
然后点击http://localhost:8000/myCustomRoute
结果将是“我是我的客户路线”
然后点击http://localhost:8000/testing/myinvitecode
结果将是“myinvitecode”
“我正在测试邀请代码路由”切换它们?切换是什么意思?将第一个作为第二个,第二个作为第一个。显然,第二个比第一个更具限制性。如果invite_代码正在“测试”它的工作,它仍然可能失败。多么愚蠢的错误啊。谢谢