Guilherme Zanini
Guilherme Zanini

Reputation: 3

Laravel Modules + VueJS, routes are being overwritten

Well, I have a problem with my routes, all requests related to the Modules folders don't work. I'm using laravel-modules package.

In my project-folder/routes/web.php I must use a wildcard route because my front is in VueJs SPA:

Route::get('/{any}', 'ApplicationController')->where('any', '.*');

Ok, I was using my API routes into the main api.php file, working fine, but when I put in Modules/ModuleName/Routes/api.php the route doesn't work.

If I remove the Route::get('any') from main web.php it works, I believe the routes are having some kind of conflict.

+-----------+------------------------------+--------------------------------------------------------------------------------+--------------+
| Method    | URI                          | Action                                                                         | Middleware   |
+-----------+------------------------------+--------------------------------------------------------------------------------+--------------+
| GET|HEAD  | api/opportunity/channel-sale | App\Modules\Opportunity\Http\Controllers\OpportunityController@channelSaleList | api,auth:api |
| GET|HEAD  | api/opportunity/flow-steps   | App\Modules\Opportunity\Http\Controllers\OpportunityController@flowStepList    | api,auth:api |
| GET|HEAD  | api/opportunity/kanban       | App\Modules\Opportunity\Http\Controllers\OpportunityController@kanbanList      | api,auth:api |
| GET|HEAD  | api/user                     | Closure                                                                        | api,auth:api |
| GET|HEAD  | {any}                        | App\Http\Controllers\ApplicationController                                     | web          |
+-----------+------------------------------+--------------------------------------------------------------------------------+--------------+

I don't know what to do.

Upvotes: 0

Views: 1154

Answers (1)

Salim Djerbouh
Salim Djerbouh

Reputation: 11044

You have two options here, either pass a regular expression to the any route to ignore API prefixed routes

Route::get('/{any}', 'ApplicationController')->where('any', '^(?!api).*$');

Or set a fallback to the ApplicationController

Route::fallback('ApplicationController');

From the docs

Fallback Routes

Using the Route::fallback method, you may define a route that will be executed when no other route matches the incoming request. Typically, unhandled requests will automatically render a "404" page via your application's exception handler. However, since you may define the fallback route within your routes/web.php file, all middleware in the web middleware group will apply to the route. You are free to add additional middleware to this route as needed:

Route::fallback(function () {
    //
});

The fallback route should always be the last route registered by your application.

Upvotes: 1

Related Questions