Laravel 意外重定向(302)

Laravel unexpected redirects ( 302 )(Laravel 意外重定向(302))
本文介绍了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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)