2017-07-27 71 views
0

路線這是我RouteServiceProvider,我已經改變了用於創建多個路由文件。創建多個文件diffrentiate在laravel 5.4

namespace App\Providers; 

use Illuminate\Routing\Router; 
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; 

class RouteServiceProvider extends ServiceProvider { 

    /** 
    * This namespace is applied to your controller routes. 
    * 
    * In addition, it is set as the URL generator's root namespace. 
    * 
    * @var string 
    */ 
    protected $namespace = 'App\Http\Controllers'; 

    /** 
    * Define your route model bindings, pattern filters, etc. 
    * 
    * @return void 
    */ 
    public function boot(Router $router) { 
     // 

     parent::boot($router); 
    } 

    /** 
    * Define the routes for the application. 
    * 
    * @return void 
    */ 
    public function map(Router $router) { 
     $this->mapApiRoutes($router); 

     $this->mapWebRoutes($router); 

     // 
    } 

    /** 
    * Define the "web" routes for the application. 
    * 
    * These routes all receive session state, CSRF protection, etc. 
    * 
    * @return void 
    */ 
    protected function mapWebRoutes($router) { 
     $router->group(['namespace' => $this->namespace, 'middleware' => 'web'], function ($router) { 
      foreach (glob(app_path('Http/Routes/Web/*.php')) as $eachRoute) { 
       require $eachRoute; 
      } 
     }); 
    } 

    /** 
    * Define the "api" routes for the application. 
    * 
    * These routes are typically stateless. 
    * 
    * @return void 
    */ 
    protected function mapApiRoutes($router) { 
     $router->group(['prefix' => 'api', 'namespace' => $this->namespace, 'middleware' => 'api'], function ($router) { 
      foreach (glob(app_path('Http/Routes/Api/*.php')) as $eachRoute) { 
       require $eachRoute; 
      } 
     }); 
    } 
} 
+0

對不起,我沒有提到的錯誤對於這一點,基本上我無法重寫引導方法,因爲在laravel 5.4,有是使用用於等作爲laravel 5.1同一目錄中創建多個路由沒有Router類 – Harry

回答

0

打開RouteServiceProvider

使用光照明\路由\路由器;聲明頂部的文件。

/** 
* Define the routes for the application. 
* 
* @return void 
*/ 
public function map(Router $router) { 
    $this->mapApiRoutes(); 

    $this->mapWebRoutes($router); 

    //used Router object above in map function 
} 

這僅適用於網絡路由,您也可以爲api創建,您需要創建目錄來區分它。

最後:

保護功能mapWebRoutes($路由器){

$router->group(['namespace' => $this->namespace], function ($router) { 

     foreach (glob(base_path('routes/web/*.php')) as $eachRoute) { 
      require $eachRoute; 
     } 
    }); 
}