2015-07-22 46 views
-1

我已經創建一箇中間件可以說AuthenticateAdmin,並在Kernel.php我已經添加了代碼:如何使用中間件來要求中間件?

'auth_admin' => \App\Http\Middleware\AuthenticateAdmin::class, 

在我的路線我有這樣的代碼:

Route::group(['middleware' => 'auth_admin'], function() { 
    // Admin routes here 
}); 

在我AuthenticateAdmin。 PHP我有這樣的代碼」

<?php namespace App\Http\Middleware; 

use Auth; 
use Closure; 
use Illuminate\Contracts\Auth\Guard; 

class AuthenticateAdmin { 

    /** 
    * Handle an incoming request. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param \Closure $next 
    * @return mixed 
    */ 
    public function handle($request, Closure $next) 
    { 
     // Perform action here 

     return $next($request); 
    } 

} 

我想要做的是,每次我用中間件‘auth_admin’,之前的‘auth_admin’中間件去,我希望它爲PErF首先使用'auth'中間件。

回答

1

你可以嘗試使用依賴注入,在構造函數中,你應該把auth中間件,然後執行的操作爲auth_admin

<?php 

namespace App\Http\Middleware; 

use Auth; 
use Closure; 
use Illuminate\Contracts\Auth\Guard; 

class AuthenticateAdmin { 
    /** 
    * Create a new authentication controller instance. 
    */ 
    public function __construct() 
    { 
     $this->middleware('auth'); 
    } 

    /** 
    * Handle an incoming request. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param \Closure $next 
    * @return mixed 
    */ 
    public function handle($request, Closure $next) 
    { 
     // Perform action here 

     return $next($request); 
    } 

} 

還有一件事,請記住遵循PSR-2標準,將命名空間放在下一行,就像我在示例中所做的一樣。

0

我不知道爲什麼你需要這樣做。但在Laravel,我覺得你可以配置像以下,使其工作:

Route::group(['middleware' => ['auth','auth_admin']], function() { 
    // Admin routes here 
}); 
相關問題