0

Inertiajs router makes requests to web.php by default, how do I change that so that it will make requests to api.php by default in a Vue app.

Route::post('/api/guardian-app/m/verify-phone', [GuardianPhoneVerificationController::class, 'verifyPhone']);
await router.post(
                "/api/guardian-app/m/verify-phone",
                {})
    ```
3
  • Can you please share your codes? Specifically your routes and the vue code that calls your APIs. Commented Apr 20 at 11:29
  • 1
    Simple add api before url. Example from users/ to api/users/. But you need to clarify the question by providing your current code. Commented Apr 21 at 1:37
  • It will look for that route in web.php, I want it to look for routes in api.php Commented Apr 21 at 16:23

1 Answer 1

0

API routes requires a header that explicitly says the requester is accepting JSON, otherwise the API will respond with redirects and HTML pages instead of API results.

The router from Inertia does not add the correct header, so you will need to implement something like Axios (or another HTTP request library) and ensure that the Accept: application/json header is sent to your API.

If you've implemented Authentication for your API routes, you will need to somehow authenticate the requests with the chosen library (with Sanctum or something similar).

Completely replacing the Inertia router defeats the entire purpose with Inertia, and you'd be better off just replacing it with Nuxt or another preferred framework.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.