【问题标题】:Laravel 5.1 route not defined redirectLaravel 5.1 路由未定义重定向
【发布时间】:2017-05-15 03:01:51
【问题描述】:

当我尝试重定向注销时需要询问错误

这是用户控制器:

public function logout()
{

    Auth::logout();
    return redirect()->route('auth/login');
}

这是我的路线:

Route::get('/', function () {
    return view('welcome');
});
// Authentication routes...
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');

// Registration routes...
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');

Route::get('testing', 'UserController@test');
Route::get('logout', 'UserController@logout');

我认为,路线上一切正常,并且我正确定义了登录(如 Laravel 文档)

但它仍然出现此错误:

InvalidArgumentException in UrlGenerator.php line 306:
Route [auth/login] not defined.

请问有什么问题吗?我有没有搞错?

非常感谢,祝你有美好的一天!

【问题讨论】:

    标签: php routes laravel-5.1


    【解决方案1】:

    route 方法需要一个名称,而不是 URI。所以你需要命名你的路线。

    像这样:

    Route::get('auth/login', ['as' => 'login', 'uses' => 'Auth\AuthController@getLogin']);
    

    或者:

    Route::get('auth/login', 'Auth\AuthController@getLogin')->name('login');
    

    现在您可以拨打return redirect()->route('login');

    请参阅Named Routes 的文档。


    或者,您可以像这样在 redirect 方法中提供 URI:

    return redirect('auth/login');
    

    虽然如果您更改此端点,这会中断。我建议命名您的路线并在您的代码中使用这些路线。

    【讨论】:

      【解决方案2】:

      将您的 return redirect()->route('auth/login'); 更改为

      return redirect('auth/login');
      

      return Redirect::to('auth/login');

      【讨论】:

      • 感谢您提供更多选择 :)
      猜你喜欢
      • 2013-09-19
      • 2021-07-19
      • 2016-01-30
      • 2016-01-24
      • 2016-01-09
      • 2023-03-15
      • 2018-06-30
      • 2018-12-16
      相关资源
      最近更新 更多