重定向到 laravel 中的路由无效

Redirect to route in laravel not working

我正在开发 Laravel 5.4.36 应用程序,其中我的路由 label 在 web.php 中定义为,

Route::get('/label/', 'LabelController@index');

但是当我尝试使用 label 从控制函数重定向到路由时,

return redirect()->route('label');

Error: InvalidArgumentException Route [label] not defined.

return Redirect::to('label');

Error: FatalErrorException Class 'App\Http\Controllers\Redirect' not found.

两者都不起作用,谁能帮我如何重定向到 Laravel 中的路由?

route() 重定向到命名路由,因此您需要在 routes/web.php:

中命名您的路由
Route::name('label')->get('/label/', 'LabelController@index');

https://laravel.com/docs/5.4/routing

您可以将路由重写为 Route::get('label', 'LabelController@index'); 然后调用 return redirect()->route('label');

将重定向调用重写为 return redirect()->route('/label/');

请试试这个

在你的routes/web.php:

Route::get('/label/', 'LabelController@index')->name('label');

在您的 LabelController 中,在函数索引的末尾:

return redirect()->route('label');

所以有很多方法可以做到这一点,但我更喜欢以下两种重定向方式。

1) 没有定义路由名称:

Route::get('/label', 'LabelController@index');
return redirect('/label');

2) 通过定义路由名称:

 Route::get('/label', 'LabelController@index')->name('label');
 return redirect()->route('label');

使用

Route::get('/label', 'LabelController@index')->name('label');

而不是

Route::get('/label/', 'LabelController@index');

删除标签后的 /。如果您不传递任何参数,则不需要 / 在路线的末尾