Laravel-如果用户未通过身份验证,如何重定向到登录 [英] Laravel - How to redirect to login if user is not authenticated

查看:92
本文介绍了Laravel-如果用户未通过身份验证,如何重定向到登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用扩展类(AdminController扩展AdminBaseController)中的__constructor,但显然它不起作用,我也不知道该怎么做,在这里您可以看到我的两个类:

I'm trying to use the __constructor from the extended class (AdminController extends AdminBaseController) but aparently it's not working and I have no idea of what can be, here you can see both of my classes:

AdminBaseController.php

AdminBaseController.php

class AdminBaseController extends Controller
{
    public function __construct(){
        if (!Auth::user()){
            return view('admin.pages.login.index');
        }
    }
}

AdminController.php

AdminController.php

class AdminController extends AdminBaseController
{
    public function __construct(){
        parent::__construct();
    }

    public function index()
    {
        return view('admin.pages.admin.index');
    }

    public function ajuda()
    {
        return view('admin.pages.admin.ajuda');
    }
}

编辑


这是我的admin路线组:

EDIT


This is my admin route group:

Route::group([
    'prefix' => 'admin',
    'middleware' => 'auth'
], function () {
    Route::get('/', 'Admin\AdminController@index');

    Route::get('login', 'Admin\AuthController@getLogin');
    Route::post('login', 'Admin\AuthController@postLogin');
    Route::get('logout', 'Admin\AuthController@getLogout');

    Route::group(['prefix' => 'configuracoes'], function () {
        Route::get('geral', 'Admin\AdminConfiguracoesController@geral');
        Route::get('social', 'Admin\AdminConfiguracoesController@social');
        Route::get('analytics', 'Admin\AdminConfiguracoesController@analytics');
    });

    Route::get('ajuda', 'Admin\AdminController@ajuda');
});

推荐答案

控制器不是检查用户是否已通过身份验证的正确位置.您应该为此使用中间件.要获取有关什么是中间件的信息,请在此处

The controller is not the right place to check if a user is authenticated or not. You should use a middleware for that. To get info on what a middleware is check here

让我们看看如何为此目的使用默认的Laravel的auth中间件:

Let's see how you can use the default Laravel's auth middleware for this purpose:

首先摆脱您的AdminBaseController,仅使用AdminController

First of all get rid of your AdminBaseController and use only AdminController

然后,您必须检查app\Http\Kernel.php

您应该有以下一行:

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,

这意味着中间件对于您的路由是活动的且可用的.

This means that the middleware is active and usable for your routes.

现在让我们进入app\Http\Middleware\Authenticate.php中的中间件类,以指定中间件的行为:

Now let's go inside the middleware class in app\Http\Middleware\Authenticate.php to specify the middleware's behaviour :

//this method will be triggered before your controller constructor
public function handle($request, Closure $next)
{
    //check here if the user is authenticated
    if ( ! $this->auth->user() )
    {
        // here you should redirect to login 
    }

    return $next($request);
}

现在剩下要做的就是确定应该应用中间件的路由.假设您有两条只希望通过身份验证的用户可以访问的路由,则应指定以这种方式将中间件用于这两条路由:

Now the only thing left to do is to decide for what routes you should apply the middleware. Let's suppose you have two routes that you want to be only accessible from authenticated users, you should specify to use the middleware for these two routes in this way:

Route::group( ['middleware' => 'auth' ], function()
{
    Route::get('admin/index', 'AdminController@index');
    Route::get('admin/ajuda', 'AdminController@ajuda');
});

这篇关于Laravel-如果用户未通过身份验证,如何重定向到登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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