1

I've seen this not-so-perfect solution where we're supposed to place a universal view route and then route everything from react-router like so:

Route::view('/{any}', 'layouts.app')->where('any', '.*');

or

Route::view('/{any?}', 'layouts.app');

But the big issue here is when I'm fetching data to react from within my laravel application, for example: /posts, react-router will try to resolve /posts, but laravel will match it with /{any?}, and the fetch call will respond with a view.

Is there a way to make apiResource calls like described, and if so, how?

1 Answer 1

1

Name your API routes with the 'api' prefix 'api/posts' (for example).
Then let your react router do the routing for the application using one universal route:

Route::view('/{any?}', 'layouts.app');

This route comes after your API routes so that your API routes don't get matched with {'/any?'} parameter. (Very important!)

Then, you can have something like this in your react app:

<Routes>
    <Route path="/posts" element={<Posts/>} />
</Routes>
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.