問題描述
我已經開始了一個新的 Laravel 5.2 項目,使用 laravel new MyApp
,并通過 php artisan make:auth
添加了身份驗證.這是一個僅限會員的網站,第一個用戶在其中被播種,然后創建其余用戶(無需手動創建用戶/密碼重置等).
I have started a new Laravel 5.2 project, using laravel new MyApp
, and added authentication via php artisan make:auth
. This is intended to be a members only website, where the first user is seeded, and creates the rest (no manual user creation/password reset/etc).
這些是我目前定義的路由:
These are the routes I have currently defined:
Route::group(['middleware' => 'web'], function () {
// Authentication Routes...
Route::get( 'user/login', ['as' => 'user.login', 'uses' => 'AuthAuthController@showLoginForm']);
Route::post('user/login', ['as' => 'user.doLogin', 'uses' => 'AuthAuthController@login' ]);
Route::group(['middleware' => 'auth'], function() {
// Authenticated user routes
Route::get( '/', ['as'=>'home', 'uses'=> 'HomeController@index']);
Route::get( 'user/{uid?}', ['as' => 'user.profile', 'uses' => 'AuthAuthController@profile' ]);
Route::get( 'user/logout', ['as' => 'user.logout', 'uses' => 'AuthAuthController@logout' ]);
Route::get( '/user/add', ['as' => 'user.add', 'uses' => 'AuthAuthController@showAddUser']);
[...]
});
});
我可以正常登錄,但是我遇到了一些非常奇怪"的行為 - 當我嘗試注銷時(通過通過 artisan 創建的內置 logout
方法),頁面做了一個 302 重定向到主頁,我仍然登錄.
I can login just fine, however I'm experiencing some very "funky" behavior - when I try to logout ( via the built-in logout
method that was created via artisan ), the page does a 302 redirect to home, and I am still logged in.
此外,雖然幾乎所有頁面(此處未列出)都按預期工作,但 user.add 也會向主頁生成 302.
What's more, while almost all pages (not listed here) work as expected, user.add also produces a 302 to the home page.
請注意主頁向 AuthController 聲明為 $redirectTo
,如果這有任何區別
Do note the homepage is declared to the AuthController as $redirectTo
, if that makes any difference
我通過調試欄發現了重定向.知道要尋找什么嗎?
I found out about the redirects via the debugbar. Any idea on what to look for ?
推薦答案
經過幾個小時的摸索,我找到了答案——這很愚蠢.
After several hours of hair pulling, I have found my answer -- and it's silly.
問題是路由 user.profile
有一個路徑 user/{uid?}
并且它匹配 user/logout
和user/add
作為路徑.
The problem is that the route user.profile
has a path user/{uid?}
and it matches both user/logout
and user/add
as paths.
它在其他人之前,沒有正則表達式或類似的,它處理了路線.
It being before the others, and not having a regex or similar, it handled the route.
我仍然不知道為什么會為 那個 頁面生成 302,但發現將其移出 AuthController
并移入 UserController代碼>(它應該從一開始的位置)修復了行為.
I still don't know why a 302 was generated for that page, but found that moving it out of the AuthController
and into the UserController
(where it should be from the start) fixed the behavior.
因此,我的(修改和工作)路線現在看起來像這樣:
Thus, my (amended and working) routes now look like so:
Route::group(['middleware' => 'web'], function () {
// Authentication Routes...
Route::get( 'user/login', ['as' => 'user.login', 'uses' => 'AuthAuthController@showLoginForm']);
Route::post('user/login', ['as' => 'user.doLogin', 'uses' => 'AuthAuthController@login' ]);
Route::group(['middleware' => 'auth'], function() {
// Authenticated user routes
Route::get( '/', ['as'=>'home', 'uses'=> 'HomeController@index']);
Route::get( '/home', ['as'=>'home', 'uses'=> 'HomeController@home']);
Route::get( 'user/logout', ['as' => 'user.logout', 'uses' => 'AuthAuthController@logout' ]);
// *** Added /profile/ here to prevent matching with other routes ****
Route::get( 'user/profile/{uid?}', ['as' => 'user.profile', 'uses' => 'UserController@profile' ]);
Route::get( '/user/add', ['as' => 'user.add', 'uses' => 'UserController@showAddUser']);
[...]
});
});
這篇關于Laravel 意外重定向(302)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!