vercel-community / php

🐘 PHP Runtime for ▲ Vercel Serverless Functions (support 7.4-8.3)

Home Page:https://php.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to do if I have two route types: /* and api/* in my Laravel Project ?

jornatf opened this issue · comments

commented

How to do if I have two route types: /* and api/* in my Laravel Project ?

I have two routes types in my Laravel Project: /* for user access dashboard and api/* for api access.

If I do like this:

    "functions": {
        "api/index.php": {
            "runtime": "vercel-php@0.5.2"
        }
    },
    "routes": [
        {
            "src": "/api/(.*)",
            "dest": "/api/index.php"
        }
    ],

The url for /api/* is /api/api/*.

How fix it ?

Thx.

Jordan

I found this on stackoverflow:
https://stackoverflow.com/questions/70534889/vercel-api-conflict-with-laravel-api

I don't try this, but maybe work for you

@jornatf

I found other way to avoid this behavior, but it make an inconsistency between local and server environment:

condition:

  • your api need to be versioned, like v1, v2, etc.

inconsistencies:

note:

the only behavior that you can change is:

To avoid this you can create one page that supply both the application access like an form to login/register and one button to reference the api documentation

changes:

RouterServiceProvider

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

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

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

api.php

Route::prefix('v1')->group(function () {
    // all your routes need to be declare here
});

Route::prefix('v2')->group(function () {
    // or here to access the v2 of your api
});

// or in both if you want segregate the resources or any other reason

web.php

// i didnt any changes