简体   繁体   English

api的Laravel多路由文件

[英]Laravel multiple routes file for api

I am working on a API for 2 client application, mobile application and angular2 admin panel.我正在为 2 个客户端应用程序、移动应用程序和 angular2 管理面板开发 API。

If I write the routes for both application in a single default routes/api.php , this will be very huge.如果我在一个默认的routes/api.php为两个应用程序编写路由,这将是非常巨大的。

So, I want to split the api routes file as :所以,我想将 api 路由文件拆分为:

  1. routes/admin.api.php for angular app角度应用程序的routes/admin.api.php

  2. routes/app.api.php for mobile app移动应用的routes/app.api.php

I have modified the RouteServiceProvide as below我修改了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'));
    }
}

I'm getting the following error我收到以下错误

(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)
  1. create two route files: admin.api.php and app.api.php .创建两个路由文件: admin.api.phpapp.api.php
  2. edit the RouteServiceProvider.php file as below:编辑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/admin.api.php');
            });
        }

    }

For more detail see here .有关更多详细信息,请参阅此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM