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

EverdevFR's avatar

API and Web on 2 different test addresses (same for deployment)

Hello,

I'm developing an API and a web application with Laravel 9. I'm currently using Laragon on my development computer and I have several questions... :)

  • How to change the test address of my API: "127.0.0.1:8000/api/" by "api.myapp.test/"?
  • How do I change the test address of my app: "127.0.0.1:8000/" to "app.myapp.test/"?
  • Apart from the detailed deployment proposed by Laravel, is it possible to block a middleware during a deployment (I want to have 1 environment for my web app and 1 environment for my api)?

Best regards, Yann

0 likes
9 replies
EverdevFR's avatar

Thanks @tray2 for your answer but it only allows me to answer my second question...

Indeed when I configure the hosts file: 127.0.0.1 app.myapp.test 127.0.0.1/api/api.myapp.test

My web app is displayed correctly but the API address does not work and that's normal... (impossible to put "/api/" in a hosts file)

I wish that in the same Laravel project, I can have api.myapp.com and app.myapp.com in deployment and that each of its web addresses send to the right middleware.

And I first want to do it on my development workstation to remove "/api/" in Laravel... But I don't know if this is possible?

And for my 3rd question: how to deactivate either the Web middleware or the API middleware during a deployment (because I'm going to use different servers and I don't want a server to be able to run the 2 middlewares at the same time)?

Tray2's avatar

@EverdevFR You only set the domain in the hosts file.

127.0.0.1 mydomain.com
127.0.0.1 api.mydomain.com
1 like
EverdevFR's avatar

Thanks @Tray2 ,

I find solution for my others problem : I simply needed to modify in RouteServiceProvider.php my access to the 2 middlewares... like this :

Route::domain('api.' . env('APP_URL')) ->middleware('api') ->group(base_path('routes/api.php'));

Route::domain('app.' . env('APP_URL')) ->middleware('web') ->group(base_path('routes/web.php'));

Thanks for all !

Sinnbeck's avatar

@EverdevFR don't use env directly in that file. Use config() instead. Try running php artisan config:cache and watch the app break completely. Run php artisan config:clear to fix it again

1 like
EverdevFR's avatar

Thank you for this information @Sinnbeck .

Reading the Laravel doc in detail, it says that env() should be used only in configuration files. But what are these files?

Sinnbeck's avatar

@EverdevFR configuration files are the files inside /config. All other files are not configured files. That file is a service provider class file

1 like

Please or to participate in this conversation.