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

vincent15000's avatar

Laravel on Ubuntu vs Laravel on Windows

Hello,

I am a member of an association (board games ;)) ... and I develop the backend of an application for this association with another member who develops the frontend.

Well ... The application is developed with Laravel / VueJS.

I work on Ubuntu and the front dev works on Windows with Herd.

The authentication is done with Fortify and I'm using Sanctum to handle the session cookie between back and front. Both back and front are on the same top level domain.

The back has an API to get the authenticated user.

return $request->user();

On Ubuntu, when the user isn't authenticated, this line returns nulll.

On Windows / Herd, when the user isn't authenticated, this line returns an HTML code.

Why this difference ?

I don't know Herd. Can it be a problem with Herd ? Something else ?

Thanks for your help.

V

0 likes
1 reply
LaryAI's avatar

The difference in behavior you’re seeing is due to how Laravel handles unauthenticated API requests, and it’s likely related to the Accept header sent by the frontend or the configuration/environment of your servers.

Explanation:

  • When you call $request->user() in a route or controller, Laravel will return the authenticated user or null if not authenticated.
  • If you have middleware like auth:sanctum on your route, and the request is unauthenticated, Laravel will return a response based on the request type:
    • If the request expects JSON (i.e., the Accept: application/json header is present), Laravel returns a JSON response (usually with a 401 status).
    • If the request does not expect JSON, Laravel returns an HTML response (often a redirect to the login page).

Why the difference?

  • On Ubuntu, your frontend (or API client) is likely sending the Accept: application/json header, so Laravel returns null (or a JSON response).
  • On Windows/Herd, the request might not be sending the Accept: application/json header, so Laravel returns an HTML response (the login page).

How to Fix:

Ensure that your frontend always sends the Accept: application/json header when making API requests. This will make Laravel consistently return JSON responses for unauthenticated requests, regardless of the OS or server.

Example (Axios):

axios.get('/api/user', {
  headers: {
    'Accept': 'application/json'
  }
})

Or, if using fetch:

fetch('/api/user', {
  headers: {
    'Accept': 'application/json'
  }
})

Summary Table:

Accept Header Authenticated Unauthenticated (Laravel Response)
application/json user object JSON error/null
(not set or text/html) user object HTML login page

Conclusion:

The issue is not with Herd or Ubuntu specifically, but with the headers sent by the frontend. Make sure your frontend always requests JSON from the API, and Laravel will respond consistently across all environments.

Please or to participate in this conversation.