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

后端 未结 2 1948
轻奢々
轻奢々 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: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.

提交回复
热议问题