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

binggle's avatar

running laravel api in different domain with frontend .

I want to run SPA / background API each in two different domain.

I used laravel/breeze api for backend and laravel/breeze-next for frontend.

And I followed steps in https://github.com/laravel/breeze-next .

backend laravel-api

in .env

APP_URL=http://localhost:8000
FRONTEND_URL=http://localhost:3000

frontend breeze-nextjs

in .env.local

NEXT_PUBLIC_BACKEND_URL=http://localhost:8000

console / run

it run in each folders.

npm run dev  # frontend

php artisan --host=localhost --port=8000 # backend 

When I fill up all input in register and submit , Webpage navigated to 'dashboard' route as expected.

It worked fine, even though it shows 401 error.

http://localhost:8000/api/user
# 401 Unauthorized

Yes, when I use same 'localhost' domain name , All works fine.

I wanted to change backend domain. So ..

in /etc/hosts

127.0.0.1       localhost  // it was existing already.
127.0.0.1       local-domain.com // it is newly added.

backend laravel-api

in .env

APP_URL=http://local-domain.com:8000
FRONTEND_URL=http://localhost:3000

in app/Http/Middleware/TrustProxies.php

class TrustProxies extends Middleware
{
    protected $proxies = ['*'];
    protected $headers =
        Request::HEADER_X_FORWARDED_FOR |
        Request::HEADER_X_FORWARDED_HOST |
        Request::HEADER_X_FORWARDED_PORT |
        Request::HEADER_X_FORWARDED_PROTO |
        Request::HEADER_X_FORWARDED_AWS_ELB;
}

in app/Http/Middleware/VerifyCsrfToken.php

class VerifyCsrfToken extends Middleware
{
    protected $except = [
        '*'
    ];
}

frontend breeze-nextjs

in .env.local

NEXT_PUBLIC_BACKEND_URL=http://local-domain.com:8000

console / run

It run in each folders again

npm run dev # frontend

php artisan --host=local-domain.com --port=8000 # backend 

when Login submit, the web page does not navigate to another page.

And it caused error repeatedly in network tab.

Request URL: http://local-domain.com:8000/api/user
Request Method: GET
Status Code: 401 Unauthorized ( with red icon)

Request URL: http://local-domain.com:8000/api/user
Request Method: OPTIONS
Status Code: 204 No Content ( with green icon)

how can I successfully /safely setup two domain for SPA / Laravel api ?

Actually I want to run nginx web server instead of 'php artisan serve'.

But I can not get over this CORS trouble yet.

Does somebody have experiences about these stuff?

0 likes
0 replies

Please or to participate in this conversation.