Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Korona123's avatar

api prefix in laravel package route

So I am developing a custom package and I want to add in some API routes. So in my package I have an src/routes/api.php (mimicking the laravel layout)

So the routes are not being prefix by the /api/{{route}}

I was wondering if there is anyway that this is 'supposed' to be done other than just setting a 'prefix' => 'api' and 'middleware' => 'api' on the route group wrapping the routes.

0 likes
4 replies
Sergiu17's avatar
Route::prefix('api')->group(function () {

    Route::group(['middleware' => ['api']], function () {
            // is this ok?
    });

});
Korona123's avatar

Yeah I mean that works. Its just odd that I need to prefix the api onto the route. The routes from the api.php file all already have the /api/ prefix applied. I was thinking there may be some way to configure that to happen in my laravel package.

I ended up just doing what you did with the prefix '/api/v1/'

It just seems like I am missing something.

1 like
richardfu71's avatar

I have the same question, have you find the way for it?

Tynael's avatar

Stumbled upon the same issue.

I ended up using the config.php to my advantage like this:


return [
    'prefix' => '/api/custom-route',
    'middleware' => ['api', 'login'],
];

To make use of the config.php in your custom package, don't forget to load them inside a ServiceProvider like this:

protected function registerRoutes()
    {
        Route::group($this->routeConfiguration(), function () {
            $this->loadRoutesFrom(__DIR__ . '/../routes/api.php');
        });
    }

    protected function routeConfiguration()
    {
        return [
            'prefix' => config('custompackage.prefix'),
            'middleware' => config('custompackage.middleware'),
        ];
    }

Please or to participate in this conversation.