2017-07-27 73 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; 
     } 
    }); 
}