vercel-community / php

🐘 PHP Runtime for ▲ Vercel Serverless Functions (support 7.4-8.3)
https://php.vercel.app
MIT License
1.26k stars 294 forks source link

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

Open jornatf opened 1 year ago

jornatf commented 1 year ago

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

ronei-kunkel commented 1 year ago

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

ronei-kunkel commented 1 year ago

@jornatf

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

condition:

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