php icon indicating copy to clipboard operation
php copied to clipboard

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

Open jornatf opened this issue 2 years ago • 2 comments

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": "[email protected]"
        }
    },
    "routes": [
        {
            "src": "/api/(.*)",
            "dest": "/api/index.php"
        }
    ],

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

How fix it ?

Thx.

Jordan

jornatf avatar May 15 '23 10:05 jornatf

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 avatar Jul 04 '23 03:07 ronei-kunkel

@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:

  • in local the endpoint to access yout versioned api is http://localhost/v1/resource
  • in server is https://your-domain/api/v1/resource

note:

the only behavior that you can change is:

  • when access https://your-domain/api have the same return when you access the https://your-domain/

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

ronei-kunkel avatar Jul 04 '23 04:07 ronei-kunkel