Laravel 5.5更改未经身份验证的登录重定向URL [英] Laravel 5.5 change unauthenticated login redirect url

查看:213
本文介绍了Laravel 5.5更改未经身份验证的登录重定向URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Laravel < 5.5中,我可以更改此文件app/Exceptions/Handler来更改未经身份验证的用户重定向url:

In Laravel < 5.5 I could change this file app/Exceptions/Handler to change the unauthenticated user redirect url:

protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }

    return redirect()->guest(route('login'));
}

但是在Laravel 5.5中,此位置已移至该位置vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php,所以现在如何更改它?我不想更改供应商目录中的内容,以防它被作曲家更新所覆盖.

But in Laravel 5.5 this has been moved to this location vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php so how can I change it now? I don't want to change stuff in the vendor directory encase it gets overridden by composer updates.

protected function unauthenticated($request, AuthenticationException $exception)
{
    return $request->expectsJson()
                ? response()->json(['message' => 'Unauthenticated.'], 401)
                : redirect()->guest(route('login'));
}

推荐答案

但是在Laravel 5.5中,该位置已移至该位置vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php,所以现在如何更改它?我不想更改vendor目录中的内容,以防它被作曲家更新所覆盖.

But in Laravel 5.5 this has been moved to this location vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php so how can I change it now? I don't want to change stuff in the vendor directory encase it gets overridden by composer updates.

只是默认情况下该功能不再存在.

It's just the case that the function is not there by default anymore.

您可以像在5.4中一样重写它.只要确保包含

You can just override it as you did in 5.4. Just make sure to include

use Exception;
use Request;
use Illuminate\Auth\AuthenticationException;
use Response;

在处理程序文件中.

例如,我的app/Exceptions/Handler.php看起来像这样:

For Example my app/Exceptions/Handler.php looks somewhat like this:

<?php
    namespace App\Exceptions;
    use Exception;
    use Request;
    use Illuminate\Auth\AuthenticationException;
    use Response;
    use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
    class Handler extends ExceptionHandler
    {
        (...) // The dfault file content
        /**
         * Convert an authentication exception into a response.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Illuminate\Auth\AuthenticationException  $exception
         * @return \Illuminate\Http\Response
         */
         protected function unauthenticated($request, AuthenticationException $exception)
         {
            return $request->expectsJson()
                    ? response()->json(['message' => 'Unauthenticated.'], 401)
                    : redirect()->guest(route('authentication.index'));
    }
}

这篇关于Laravel 5.5更改未经身份验证的登录重定向URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆