Как настроить URL перенаправления при тайм-ауте сессии - Laravel 5.8? - PullRequest
0 голосов
/ 21 сентября 2019

Когда время ожидания сеанса - я продолжаю перенаправлять на: 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);
    }

}

Ответы [ 2 ]

1 голос
/ 21 сентября 2019

Согласно документации Laravel, вам нужно определить этот метод только с патом, на который вы хотите перенаправить ваше Authenticate.php Middleware:

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

Дополнительная информация здесь в Перенаправление неаутентифицированных пользователей параграф.

0 голосов
/ 21 сентября 2019
    <?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');
        }
    }
}
...