2017-07-04 102 views
1

我正在爲2客戶端應用程序,移動應用程序和angular2管理面板提供API。Laravel api的多個路徑文件

如果我在一個默認的routes/api.php中編寫兩個應用程序的路由,這將是非常巨大的。

所以,我要拆分的API路線文件:

  1. routes/admin.api.php的角度應用

  2. routes/app.api.php移動應用

我已經修改了RouteServiceProvide如下

<?php 

namespace App\Providers; 

use Illuminate\Support\Facades\Route; 
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; 

class RouteServiceProvider extends ServiceProvider 
{ 
    protected $namespace = 'App\Http\Controllers'; 

    public function boot() 
    { 
     // 

     parent::boot(); 
    } 

    public function map() 
    { 
     $this->mapAdminApiRoutes(); 

     $this->mapApiRoutes(); 

     $this->mapWebRoutes(); 

     // 
    } 

    protected function mapWebRoutes() 
    { 
     Route::middleware('web') 
      ->namespace($this->namespace) 
      ->group(base_path('routes/web.php')); 
    } 

    protected function mapApiRoutes() 
    { 
     Route::prefix('api/v1') 
      ->middleware('api') 
      ->namespace($this->namespace) 
      ->group(base_path('routes/api.php')); 
    } 

    protected function mapAdminApiRoutes() 
    { 
     Route::prefix('api/v1') 
      ->middleware('api') 
      ->namespace($this->namespace) 
      ->group(base_path('routes/admin.api.php')); 
    } 
} 

我收到以下錯誤

(1/1) FatalErrorException 
Illuminate\Routing\Router::loadRoutes(): Failed opening required 'D:\Workspace\Project Izzmart\izzmart\routes/api.php' (include_path='.;C:\php\pear') 
in Router.php (line 329) 
+0

我不知道,但我有佛對RouteServiceProvider中'api.php'路由的引用,嘗試將其添加到現有require的下方 –

+0

[多路由文件可能位於laravel 5中的一個主路徑文件的位置](https://stackoverflow.com/問題/ 34182806 /多路由文件是一個主路由文件在laravel-5) – linktoahref

+0

檢查路徑文件的存儲路徑! – linktoahref

回答

1
  1. 創建兩個途徑文件admin.api.php和app.api.php。

  2. 編輯RouteServiceProvider.php文件到如下:

<?php 
 

 
namespace App\Providers; 
 

 
use Illuminate\Support\Facades\Route; 
 

 

 
class RouteServiceProvider extends ServiceProvider 
 
{ 
 
    protected $namespace = 'App\Http\Controllers'; 
 
    protected $apiNamespace = 'App\Http\Controllers\Api\v1'; 
 

 
    public function boot() 
 
    { 
 

 
     parent::boot(); 
 
    } 
 

 
    public function map(Router $router) { 
 
     $router->group(['namespace' => $this->namespace], function ($router) { 
 
      require app_path('Http/routes/web.php'); 
 
     }); 
 
     $router->group(['namespace' => $this->apiNamespace], function ($router) { 
 
      require app_path('Http/api.php'); 
 
     }); 
 
     $router->group(['namespace' => $this->apiNamespace], function ($router) { 
 
      require app_path('Http/amin.api.php'); 
 
     }); 
 
    } 
 

 
}

有關更多詳情,請here

http://laravel-tricks.com/tricks/laravel-5-multiple-routes-files

+0

感謝您的時間真的很感激它,我已更新我的文章,請問我可以告訴我什麼我失蹤 –

+0

我已經更新了答案,試試這個 –