中间件在laravel除了每隔一个HTTP请求到应用程序5.1

问题描述:

我在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

您可以使用路由组和中间件分配给它:

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

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

不要对中间件做任何事情。您可以在路线组之外*选择该路线。所以它成为一个独立的路线。或者,您可以创建一个新的路由组,并且仅在没有该中间件的情况下放入一条路由。例如。

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

与此中间件仅影响第二路由组

有一对夫妇的方式来解决这个问题,一种是在你的中间件中解决这个问题,并在那里排除这条路由,另外两条是将你想要在你的中间件中覆盖的所有路由分组到你的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 
});