【问题标题】:Redirect after authentication in Laravel 5在 Laravel 5 中进行身份验证后重定向
【发布时间】:2016-07-25 11:52:30
【问题描述】:

在 Laravel 5 中成功登录后,我想将用户重定向回他们在登录页面之前查看的页面。目前,如果用户输入错误的用户名/密码,他将再次重定向到登录页面,并显示错误,我想保持这种状态。此外,我希望当用户单击页面上的登录按钮以及用户被自动重定向到登录页面的情况下,它也能正常工作。

在我看来

protected $redirectTo = "/";

是这里的关键,但我不知道如何让它“重定向到不是登录页面的最后一页”。任何帮助表示赞赏。

【问题讨论】:

    标签: php laravel redirect


    【解决方案1】:
    $url = URL::previous() != url('login') ? URL::previous() : null;
    $previousUrl = old('previousUrl', $url);
    

    $previousUrl 传递到您的登录视图。

    在您的登录视图中输入previousUrl 字段:<input type="hidden" name="previousUrl" value="{{$previousUrl}}"/>

    在您的AuthController.php 更新构造函数中:

    public function __construct()
    {
        $this->redirectTo = app('request')->input('previousUrl') ? : $this->redirectTo;
        <...>
    }
    

    未经测试,但应该可以。

    【讨论】:

    • 等一下,我应该有:$url = URL::previous() != url('login') ? URL::previous() : null; $previousUrl = old('previousUrl', $url); 在我希望这成为可能的每个视图中?
    • 不,将该代码放入AuthController,返回视图并将$previousUrl附加到该视图。
    • 我觉得自己很蠢,但是,这到底是哪个函数?这是我的 AuthController 的样子pokit.org/get/img/692be56a2778fa74ab98a9b51646bbdd.jpg
    • 创建getLogin 方法覆盖。原始方法位于 AuthenticatesUsers 特征中。这就是你的方法应该看起来像pastebin.com/KNd1vsCi
    • 如何覆盖 getLogin 方法?我尝试在供应商文件夹中编辑它,但我知道我们不应该这样做,但它仍然没有工作。
    【解决方案2】:

    一旦您知道登录成功,请在重定向中使用 URL::previous() 或 return redirect()-&gt;back();

    此外,您可以将 url 保存在会话中,以便以后随时使用。

    【讨论】:

    • 我猜return redirect()-&gt;back() 会尝试重定向到登录页面。
    • 如果之前登录失败,是的。
    【解决方案3】:

    Laravel 允许您覆盖 AuthController 中的 redirectPath() 方法,该方法通常会使用 redirectTo 属性,但您可以将其设置为动态。

    public function redirectPath()
    {
        return request('redirect');
    }
    

    并在登录刀片模板中放置一个隐藏的input

    <input type="hidden" name="redirect" value="{{ url()->previous() }}">
    

    【讨论】:

    • 我还有一个问题,如果用户忘记了他的密码一次,那么之前的 url 的数据不会保存在任何地方。
    【解决方案4】:

    试试:

    redirect()->intended();
    

    【讨论】:

      【解决方案5】:

      Laravel 5.2

      当用户进入您的登录页面时,您必须存储之前的 URL: https://laravel.com/docs/5.2/helpers#method-url

      会话是一个选项:https://laravel.com/docs/5.2/helpers#method-session

      之后,您应该在用户尝试登录时使用该信息填充 $redirectTo:https://laravel.com/docs/5.2/authentication#included-authenticating

      我用中间件写了一个例子。

      首先你需要创建一个:

      php artisan make:middleware BeforeLoginForm
      

      现在我们需要覆盖 Auth\AuthController@showLoginForm 的默认路由,因此我们可以将中间件添加到该路由:

      app/Http/routes.php

      Route::auth();
      
      $this->get('login', 'Auth\AuthController@showLoginForm')
          ->middleware('before.loginform');
      

      为我们的中间件定义一个别名:

      app/Http/Kernel.php

      protected $routeMiddleware = [
          'auth' => \App\Http\Middleware\Authenticate::class,
          'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
          'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
          'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
          'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
          'before.loginform' => \App\Http\Middleware\BeforeLoginForm::class,
      ];
      

      我们的中间件将使用会话助手存储之前的 URL:

      app/Http/Middleware/BeforeLoginForm.php

      namespace App\Http\Middleware;
      
      use Closure;
      
      class BeforeLoginForm
      {
          /**
           * Handle an incoming request.
           *
           * @param  \Illuminate\Http\Request  $request
           * @param  \Closure  $next
           * @return mixed
           */
          public function handle($request, Closure $next)
          {
              session()->put('before_login_url', url()->previous());
              return $next($request);
          }
      }
      

      AuthController 的构造函数将使用我们之前存储的 URL 更改 redirectTo 属性:

      app/Http/Controllers/Auth/AuthController.php

      <?php
      
      namespace App\Http\Controllers\Auth;
      
      class AuthController extends Controller
      {
      
          /**
           * Where to redirect users after login / registration.
           *
           * @var string
           */
          protected $redirectTo = '/';
      
          /**
           * Create a new authentication controller instance.
           *
           * @return void
           */
          public function __construct()
          {
              $redirectTo = session()->get('before_login_url');
              if (!empty($redirectTo)) {
                  $this->redirectTo = $redirectTo;
              }
              $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
          }
      

      对于 Laravel 5.0(只有不同​​之处)

      你应该像这样注册你的路线:

      app/Http/routes.php

      Route::get('/', 'WelcomeController@index');
      Route::get('home', 'HomeController@index');
      Route::controllers([
              'auth' => 'Auth\AuthController',
              'password' => 'Auth\PasswordController',
      ]);
      
      Route::get('auth/login', [
          'middleware' => 'before.loginform',
          'uses' => 'Auth\AuthController@getLogin'
      ]);
      

      然后在你的 AuthController 上修改 redirectTo:

      app/Http/Controllers/Auth/AuthController.php

      public function __construct(Guard $auth, Registrar $registrar)
      {
          $this->auth = $auth;
          $this->registrar = $registrar;
      
          $redirectTo = session()->get('before_login_url');
          if (!empty($redirectTo)) {
              $this->redirectTo = $redirectTo;
          }
      
          $this->middleware('guest', ['except' => 'getLogout']);
      }
      

      我无法测试,稍后再做。

      注意,因为“redirectTo”属性的值不仅会影响身份验证,还会影响注册过程。

      【讨论】:

      • 登录视图之前的控制器到底在哪里,处理输入之后所做的事情的控制器在哪里(我假设这个是 AuthController)?
      • 对不起,在我更新答案之前我没有看到你的问题。您可以在操作之前使用中间件来处理任何事情。
      • 这个方法看起来不错,但是我使用的是 Laravel 5.0,所以我的文件看起来不一样。这是我的 AuthController pokit.org/get/?c9877ac27dd8134ee5787c0bfc8e98d9.jpg,这是我的路由文件:pokit.org/get/?124ec6ff4349413eb32f7d57cea1ab60.jpg。我通过直截了当地复制粘贴尝试了您的方法,但没有成功。我也试图变得更聪明一点,但仍然无法让它工作,我认为这是因为这两个文件,我不确定该怎么做。
      • 我编辑了 Laravel 5.0 的答案,但我还不能测试它。
      • 很抱歉打扰了,但仍然无法正常工作,并且在您为此付出的所有努力下,我真的想给您一颗星。我的 AuthController 中没有函数 showLoginForm,当中间件被称为 BeforeLoginForm 时,我也对你对“before.loginform”的使用感到困惑,也许是错字?在当前状态下,我认为中间件完全被跳过了。
      猜你喜欢
      • 2021-11-27
      • 2017-08-19
      • 2016-05-13
      • 2017-12-24
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 2021-02-08
      • 2011-04-30
      相关资源
      最近更新 更多