2016-05-24 1 views
3

のルートでWebミドルウェアを回避/削除このchangesがLaravel 5.2.31以上の場合、app/Http/routes.phpのすべてのルートがWebミドルウェアグループに分類されます。Laravel> = 5.2.31

protected function mapWebRoutes(Router $router) 
{ 
    $router->group([ 
     'namespace' => $this->namespace, 'middleware' => 'web', 
    ], function ($router) { 
     require app_path('Http/routes.php'); 
    }); 
} 

RouteServiceProvider.phpで質問:

  1. webミドルウェアなしルートのセットを定義する最も簡単な/最良の方法は何ですか?私はこれを解決
  2. このためのユースケース

一つにされ、セッションミドルウェアなしステートレスAPIのルートを宣言すると、ウェブ基ミドルウェア該当

答えて

7

一つの方法は、app/Providers/RouteServiceProvider.phpを編集し、他のための別のルートファイルを有することですグループミドルウェアすなわちAPI

public function map(Router $router) 
{ 
    $this->mapWebRoutes($router); 
    $this->mapApiRoutes($router); 

    // 
} 

protected function mapWebRoutes(Router $router) 
{ 
    $router->group([ 
     'namespace' => $this->namespace, 'middleware' => 'web', 
    ], function ($router) { 
     require app_path('Http/routes.php'); 
    }); 
} 

// Add this method and call it in map method. 
protected function mapApiRoutes(Router $router) 
{ 
    $router->group([ 
     'namespace' => $this->namespace, 'middleware' => 'api', 
    ], function ($router) { 
     require app_path('Http/routes-api.php'); 
    }); 
} 

は、結果を検証し、端末上のphp artisan route:listを実行し、ルートミドルウェアを確認します。例えばについては

Now I have some route without web middleware which is defined in different file which later called in RouteServiceProvider

今、私があなたならば、後でRouteServiceProvider

OR

と呼ばれる別のファイルに定義されているウェブミドルウェアなしでいくつかのルートを持っています古い機能を好むなら、あなたは次のようなものを持つことができます:

public function map(Router $router) 
{ 
    $this->mapWebRoutes($router); 
    $this->mapGeneralRoutes($router); 
} 

protected function mapGeneralRoutes(Router $router) 
{ 
    $router->group(['namespace' => $this->namespace], function ($router) { 
     require app_path('Http/routes-general.php'); 
    }); 
} 

その後、routes-general.phpであなただけのこれは実際Laravel 5.3用の箱から出てきている

+0

前のような路線の異なるセットに対して複数のミドルウェア・グループを持つことができます。今、webとapiの2つのファイルがあります – geckob