簡體   English   中英

Laravel - 如果用戶未經過身份驗證,如何重定向到登錄

[英]Laravel - How to redirect to login if user is not authenticated

我正在嘗試使用擴展類中的__constructorAdminController擴展AdminBaseController ),但顯然它不起作用,我不知道它可以是什么,在這里你可以看到我的兩個類:

AdminBaseController.php

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

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路由組:

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');
});

控制器不是檢查用戶是否經過身份驗證的正確位置。 你應該使用中間件。 要獲取有關中間件檢查的信息,請訪問此處

讓我們看看如何使用默認的Laravel的auth中間件來實現此目的:

首先擺脫AdminBaseController並僅使用AdminController

然后你必須檢查文件app\\Http\\Kernel.php是否啟用了auth中間件

你應該有這條​​線:

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

這意味着中間件是活動的並且可用於您的路由。

現在讓我們進入app\\Http\\Middleware\\Authenticate.php的中間件類來指定中間件的行為:

//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);
}

現在唯一要做的就是決定應該應用中間件的路由。 假設您有兩條路徑只能從經過身份驗證的用戶訪問,您應該指定以這種方式使用這兩條路由的中間件:

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

為此目的使用中間件,然后在控制器構造函數中使用它,如下例所示。

public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}

然后,您需要保護您希望用戶登錄的路由以進行訪問。

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

在Laravel 5.5中,未經身份驗證的用戶將導致Authenticate中間件拋出AuthenticationException異常。

protected function authenticate(array $guards)
{
 if (empty($guards))
 {
  return $this->auth->authenticate();
 }
 foreach ($guards as $guard) {
  if ($this->auth->guard($guard)->check()) {
      return $this->auth->shouldUse($guard);
  }
 }
 throw new AuthenticationException('Unauthenticated.', $guards);
}

這將由app / Exceptions / Handler類捕獲,該類將調用其render方法,該方法負責將給定異常轉換為HTTP響應。

public function render($request, Exception $exception)
{
 return parent::render($request, $exception);
}

App / Exceptions / Handler擴展了'Illuminate \\ Foundation \\ Exceptions \\ Handler',位於'/ vendor / laravel / src / Illuminate / Foundation / Exceptions / Handler'中。 它有自己的渲染方法。 在該render方法中,有一個if else語句。

elseif ($e instanceof AuthenticationException)
{
 return $this->unauthenticated($request, $e);
}

下面是上面在同一個類中調用的'unauthenticated'方法

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

在此方法中,您可以重定向未經身份驗證的用戶。

據我所知,這是幕后發生的事情。

擴展和執行父縮寫器的方式是正確的,但是只能從路由,控制器操作和過濾器返回執行它的視圖。 否則你必須調用send()。

為了你的目的,我認為你應該使用之前的過濾器http://laravel.com/docs/4.2/routing#route-filters

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM