【问题标题】:How to configure the redirect URL when session timeout - Laravel 5.8?会话超时时如何配置重定向 URL - Laravel 5.8?
【发布时间】:2019-09-21 12:34:33
【问题描述】:

当会话超时 - 我一直重定向到:http://bheng.test/login

导致崩溃

如何改写此行为以重定向到:http://bheng.test

这是我的 AuthMiddleware.php

<?php

namespace App\Http\Middleware;
use Closure, View;
use Illuminate\Contracts\Auth\Guard;

class Authenticate {

    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;


    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @return void
     */
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->auth->guest())
        {
            if ($request->ajax())
            {
                return response('Unauthorized.', 401);
            }
            else
            {
                return redirect()->guest('/');
                // return response('Unauthorized.', 401);
                // return View::make('layouts.share.errors.404');
            }
        }

        return $next($request);
    }

}

【问题讨论】:

  • 错误信息与会话超时有很大不同。

标签: php laravel laravel-5 middleware laravel-5.8


【解决方案1】:

根据 Laravel 文档,您只需在 Authenticate.php 中间件中使用要重定向到的 pat 定义此方法:

/**
 * Get the path the user should be redirected to.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return string
 */
protected function redirectTo($request)
{
    return route('login');
}

更多信息here重定向未经身份验证的用户段落下。

【讨论】:

    【解决方案2】:
        <?php
    
    namespace App\Http\Middleware;
    
    use Illuminate\Auth\Middleware\Authenticate as Middleware;
    
    class Authenticate extends Middleware
    {
        /**
         * Get the path the user should be redirected to when they are not authenticated.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return string
         */
        protected function redirectTo($request)
        {
            if (! $request->expectsJson()) {
                return route('login');
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-09
      • 2017-12-27
      • 1970-01-01
      • 2013-06-08
      • 2014-06-23
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多