dongyounai6281 2017-07-04 06:40
浏览 247
已采纳

Laravel多个路由文件为api

I am working on a API for 2 client application, mobile application and angular2 admin panel.

If I write the routes for both application in a single default routes/api.php, this will be very huge.

So, I want to split the api routes file as :

  1. routes/admin.api.php for angular app

  2. routes/app.api.php for mobile app

I have modified the RouteServiceProvide as below

<?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'));
    }
}

I'm getting the following error

(1/1) FatalErrorException
Illuminate\Routing\Router::loadRoutes(): Failed opening required 'D:\Workspace\Project Izzmart\izzmartoutes/api.php' (include_path='.;C:\php\pear')
in Router.php (line 329)
  • 写回答

1条回答 默认 最新

  • douqiao3453 2017-07-04 06:54
    关注
    1. create two route files: admin.api.php and app.api.php.

    2. edit the RouteServiceProvider.php file as below:

    <?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/admin.api.php');
            });
        }
    
    }

    For more detail see here.

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化