2016-02-26 23 views
0

我在laravel新的,我要創建我的應用程序中運行(我不想使用laravel默認登錄系統)中間件在laravel除了每隔一個HTTP請求到應用程序5.1

我想用中間件在我的應用程序每一個HTTP請求中,除了一個

在laravel 5.1機制的文檔syas運行,我可以使用全球中間件但我想不使用中間件只需登錄頁面。 我該怎麼辦? 這是我的中間件:

<?php 

namespace App\Http\Middleware; 

use Closure; 

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

     if(! session()->has('Login') ) 
     { 
      return redirect('login'); 
     } 

     return $next($request); 
    } 
} 
+0

告訴我們你的'routes.php'頁 – Derek

回答

1

您可以使用路由組和中間件分配給它:

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

// Special routes which you dont want going thorugh the this middleware goes here 
1

不要對中間件做任何事情。您可以在路線組之外自由選擇該路線。所以它成爲一個獨立的路線。或者,您可以創建一個新的路由組,並且僅在沒有該中間件的情況下放入一條路由。例如。

Route::group(['prefix' => 'v1'], function() { 
    Route::post('login','AuthenticationController'); 
}); 

Route::group(['prefix' => 'v1', 'middleware' => 'web'], function() { 
    Route::resource('deparments','AuthenticationController'); 
    Route::resource("permission_roles","PermissionRolesController"); 
}); 

與此中間件僅影響第二路由組

1

有一對夫婦的方式來解決這個問題,一種是在你的中間件中解決這個問題,並在那裏排除這條路由,另外兩條是將你想要在你的中間件中覆蓋的所有路由分組到你的routes.php中,然後在你的分組之外擁有那些你想排除的路由。

解決這個中間件

只需修改handle功能包括if語句檢查URI請求

public function handle($request, Closure $next) 
{ 
    if ($request->is("route/you/want/to/exclude")) 
    { 
     return $next($request); 
    } 

    if(! session()->has('Login') ) 
    { 
     return redirect('login'); 
    } 

    else 
    { 
      return redirect('login'); 
    } 
} 

此方法允許您設置中間件了全球中間件,你可以通過將if語句擴展爲or $request->is()來進行多個排除。

解決這個路線中

//Place all the routes you don't want protected here 

Route::group(['middleware' => 'admin'], function() { 
    //Place all the routes you want protected in here 
}); 
相關問題