1

I am trying to deploy my Laravel app on Vercel.

Here is the RouteServiceProvider.php code:

...
public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::prefix('apis')
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));

            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));
        });
    }
...

The code in my api.php:

Route::get('/', function () {
    return 'this is an API';
});

Route::get('/person', function () {
    $person = [
        'first_name' => 'Sean',
        'last_name' => 'Pooley',
    ];
    return $person;
});

At local env, localhost/api and localhost/api/person, works fine but when I deploy it on Vercel, the api myapp/api/person doesn't work but myapp/apis/person, myapp/apis/, myapp/api display what I want.

I suspect that the route is always refer to Vercel instead of Laravel route because there is one directory api at the root, so whenever calling for api it will direct to that folder. So is there anyway to overcome this issue? Or only can use apis as the prefix at the Laravel?

2
  • so you have a folder named 'api' in your 'public' folder? Commented Dec 30, 2021 at 20:12
  • No, the api folder is at the root directory, which have a index.php file to tell vercel to forward request to normal index.php (at public/index.php). Why would you ask that? (curious Commented Dec 31, 2021 at 3:34

2 Answers 2

3

On Vercel, the /api directory is reserved for Serverless functions. There are some workarounds, if you still want to use that route for other code, like prefixing the route with an underscore _ . Read docs here

Sign up to request clarification or add additional context in comments.

Comments

3

I was experiencing the same issue and found out that by removing the api prefix from RouteServiceProvider.php you can access both /resource and /api/resource endpoints.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.