php Laravel 意外重定向(302)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35020477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:19:08  来源:igfitidea点击:

Laravel unexpected redirects ( 302 )

phplaravel-5

提问by Nick Andriopoulos

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).

我已经开始了一个新的 Laravel 5.2 项目,使用laravel new MyApp,并通过php artisan make:auth. 这旨在成为仅限会员的网站,其中第一个用户被播种,并创建其余用户(无需手动创建用户/密码重置/等)。

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']);

    [...]
  });
});

I can login just fine, however I'm experiencing some very "funky" behavior - when I try to logout ( via the built-in logoutmethod that was created via artisan ), the page does a 302 redirect to home, and I am still logged in.

我可以正常登录,但是我遇到了一些非常“时髦”的行为 - 当我尝试注销时(通过logout通过 artisan 创建的内置方法),页面执行 302 重定向到主页,我仍然登录。

What's more, while almost all pages (not listed here) work as expected, user.add also produces a 302 to the home page.

更重要的是,虽然几乎所有页面(此处未列出)都按预期工作,但 user.add 也会向主页生成 302。

Do note the homepage is declared to the AuthController as $redirectTo, if that makes any difference

请注意主页被声明为 AuthController as $redirectTo,如果这有任何区别

I found out about the redirects via the debugbar. Any idea on what to look for ?

我通过调试栏发现了重定向。知道要找什么吗?

采纳答案by Nick Andriopoulos

After several hours of hair pulling, I have found my answer -- and it's silly.

经过几个小时的头发拉扯,我找到了我的答案——这很愚蠢。

The problem is that the route user.profilehas a path user/{uid?}and it matches both user/logoutand user/addas paths.

问题是该路线user.profile有一条路径,user/{uid?}并且它与user/logoutuser/add作为路径都匹配。

It being before the others, and not having a regex or similar, it handled the route.

它在其他人之前,并且没有正则表达式或类似的,它处理了路线。

I still don't know why a 302 was generated for thatpage, but found that moving it out of the AuthControllerand into the UserController(where it should be from the start) fixed the behavior.

我仍然不知道为什么会为该页面生成 302 ,但发现将其移出AuthController和移入UserController(它应该从一开始的位置)修复了该行为。

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']);

    [...]
    });
});

回答by mistajolly

I encountered an issue with 302 Redirects when posting ajax requests. The solution in this case was to remember to include the CSRF token.

我在发布 ajax 请求时遇到了 302 重定向问题。这种情况下的解决方案是记住包含 CSRF 令牌。

See the Laravel 5.4 documents here: https://laravel.com/docs/5.4/csrf

在此处查看 Laravel 5.4 文档:https://laravel.com/docs/5.4/csrf

回答by James

I got the same issue and i solved it by adding the header with accept:'application/json'. And I think I checked the source code before which indicates that if you don't add this, it might redirect when you are using the auth middleware. But I am not sure if it is the case and I cannot recall where i found this.

我遇到了同样的问题,我通过添加带有 accept:'application/json' 的标头解决了它。而且我想我之前检查了源代码,它表明如果您不添加它,它可能会在您使用 auth 中间件时重定向。但我不确定是否是这种情况,我不记得我在哪里找到的。

回答by Pedram Vdl

I had this issue and it turned out I had a route:redirect inside my ajax controller. which doesn't make sense because obviously we have to return ajax but I was returning a route!

我遇到了这个问题,结果我的 ajax 控制器中有一个 route:redirect 。这是没有意义的,因为显然我们必须返回 ajax 但我返回的是一条路线!

回答by smartrahat

May be default redirect page after logout is homeand seems like you do not have homein your webroute. Try the below code in your AuthController.php

注销后可能是默认重定向页面,home并且似乎home您的web路线中没有。试试下面的代码AuthController.php

use AuthenticatesAndRegistersUsers, ThrottlesLogins; // after this line
$redirectAfterLogout = 'login' // add this line

This will redirect you to loginpage after logout. You can change it to any route if you wish. I used loginas an example.

这会将您重定向到login注销后的页面。如果您愿意,您可以将其更改为任何路线。我login举了个例子。

OR

或者

You can change after logout route in \vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php

您可以在注销后更改路由 \vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php

public function logout()
    {
        Auth::logout();

        return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : 'login');
    }

I changed the default route to login. If you don't have $redirectAfterLogoutin your AuthController.phpit will look here for redirect path. I don't suggest people to edit here, it's kind of hard coding.

我将默认路由更改为login. 如果你没有$redirectAfterLogout在你的AuthController.php就看看这里的重定向路径。我不建议人们在这里编辑,这是一种硬编码。

回答by vkGunasekaran

I too experienced this issue in the login page which worked fine previously. So thought have a look at the directory permissions and it resulted the following:

我在以前运行良好的登录页面中也遇到了这个问题。所以想看看目录权限,结果如下:

drwxr-xr-x  7 user user     4096 Jun 27  2019 storage

So storage directory has 755 permission which means only the owner has write access, Since that directory owned by "user" others like laravel can't write into it.

所以存储目录有 755 权限,这意味着只有所有者有写访问权限,因为“用户”拥有的目录,像 laravel 这样的其他人不能写入它。

Changing the directory to 777 with this cmd resolved my issue:

使用此 cmd 将目录更改为 777 解决了我的问题:

sudo chmod 777 -R PROJECT_PATH/storage

The right way,

正确的方式,

Since making that directory world-writable isn't the right way, make that directory owned by apache and set 775 to storage.. and it worked again.

由于使该目录可全局写入不是正确的方法,因此将该目录设为 apache 所有并将 775 设置为 storage.. 并再次运行。

sudo chown -R user:www-data PROJECT_PATH/storage
sudo chmod 775 -R PROJECT_PATH/storage