Laravel 意外重定向(302) [英] Laravel unexpected redirects ( 302 )

查看:73
本文介绍了Laravel 意外重定向(302)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始了一个新的 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/logoutuser/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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆