0

I'm building a SPA with Laravel and Vue. However, I need to make an API call with axios but it's not working.

Here's my web.php:

Route::get('/{any}', function () {
    return view('index');
})->where('any', '.*');

Here's my api.php route:

Route::get('/notification', 'NotificationController@index');

Whenever I make an API call, it shows a blank or broken page.

Could you please give me any suggestion about this issue and solve?

3
  • 1
    In web.php replace your Route::get('/{any}'... with Route::fallback laravel.com/docs/8.x/routing#fallback-routes Commented Jun 21, 2021 at 11:10
  • @Guru But by doing this, I'm getting: UnexpectedValueException: Invalid route action: [App\Http\Controllers\/{any}]. Commented Jun 21, 2021 at 11:25
  • 1
    You don't need {any} just Route::fallback(function(){return ...}) Commented Jun 22, 2021 at 12:39

1 Answer 1

2

You should prevent api routes from being captured by web.php route.

First, prefix api routes:

api.php

Route::get('/api/notification', 'NotificationController@index');

Then, exclude prefixed routes from web routes:

web.php

Route::get('/{any}', function () {
    return view('index');
})->where('any', '^(?!api).*$');
Sign up to request clarification or add additional context in comments.

2 Comments

Do I need to prefix it with /api in api.php or in web.php?
only api routes in api.php

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.