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

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

问题描述

我已经使用laravel new MyApp启动了一个新的Laravel 5.2项目,并通过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' => 'Auth\AuthController@showLoginForm']);
  Route::post('user/login',  ['as' => 'user.doLogin',   'uses' => 'Auth\AuthController@login'        ]);

  Route::group(['middleware' => 'auth'], function() {
    // Authenticated user routes
    Route::get( '/', ['as'=>'home', 'uses'=> 'HomeController@index']);
    Route::get( 'user/{uid?}', ['as' => 'user.profile',   'uses' => 'Auth\AuthController@profile' ]);
    Route::get( 'user/logout', ['as' => 'user.logout',    'uses' => 'Auth\AuthController@logout'  ]);
    Route::get( '/user/add',   ['as' => 'user.add',       'uses' => 'Auth\AuthController@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.

请注意,如果首页有任何区别,则将首页声明为

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' => 'Auth\AuthController@showLoginForm']);
  Route::post('user/login',  ['as' => 'user.doLogin',   'uses' => 'Auth\AuthController@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' => 'Auth\AuthController@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天全站免登陆