Is it possible to create a second Laravel “api route” with a separate API KEY?

后端 未结 2 1945
轻奢々
轻奢々 2021-01-06 06:31

I\'m new to Laravel and I am handed an existing application that is composed of two parts:

1 - An admin backend built on Laravel and uses Vueify

2 - The fron

相关标签:
2条回答
  • 2021-01-06 06:36

    You need to define a new route file, firstly add a new entry $this->mapApi2Routes(); in the map() function in app\Providers\RouteServiceProvider.

    Then add a new function in that file, basically copying the mapApiRoutes() function, call it mapApi2Routes(). You can use different middleware etc. for the new file.

    The last step would be adding a new file api2.php in the routes folder.

    0 讨论(0)
  • 2021-01-06 06:43

    You can decompose routes in as many files as you want, you can also give each file its own prefix (like how api.php routes start with /api)

    The modification need to be done in App\Providers\RouteServiceProvider

    //in map() add $this->mapApiTwoRoutes()
    public function map()
    {
        $this->mapApiRoutes();
        $this->mapApiTwoRoutes();//<---this one
        $this->mapWebRoutes();
    }
    
    //now add the method mapApiTwoRoutes
    protected function mapApiTwoRoutes()
    {
        Route::prefix('api2')//<-- prefix in the url
             ->middleware('api')//<-- api middleware (throttle and such check App\Http\Kernal.php)
             ->namespace('App\Http\Controllers') //<-- you can modify the namespace of the controllers 
             ->group(base_path('routes/apiTwo.php'));//<-- file containing the routes
    }
    

    And that's it.

    0 讨论(0)
提交回复
热议问题