13
votes

I need to catch all routes, except those that have /api/ segment in them. I know how to catch every single route

Route::any('{all}', 'AngularController@serveFrontend')->where('all', '(.*)');

But what do I need to change so that my api routes aren't captured by this string ?

4

4 Answers

26
votes

You can catch all routes where the path does not start with api

Route::any('{all}', 'AngularController@serveFrontend')->where('all', '^(?!api).*$');

Or simply leave your catchall as the last route and it'll work as expected.

2
votes

Just place route(s) with /api/ segment before this one and it will work as you want. All API related URLs will be processes by first route, everything else will be processed with second one.

1
votes

You can catch all the routes except those that start /api

Route::any('{any}', [UserController::class,'index'])->where('any', '^(?!api).*$');
1
votes

For multiple exceptions, you may use | identifier: (eg nova)

Route::get('/{all}','AngularController@serveFrontend')->where('all','^(?!api|nova).*$');