nWidart / laravel-modules

Module Management In Laravel
https://docs.laravelmodules.com
MIT License
5.52k stars 959 forks source link

Api Routes Wont hit controller #1259

Closed apps4u closed 3 years ago

apps4u commented 3 years ago

Hi, I have a Laravel 7 Application with 3 modules, I can enable all 3 and I can list routes and all my module routes show up, But for whatever reason, I can not run them when I call the routes and set breakpoints they never get to the controller action at all, I've tried just about everything I can think of I have tried test route with closure and they don't work.

This is my Route Service Provider. I

    protected function mapApiRoutes()
    {
        Route::prefix('api')
            ->middleware('api')
            ->namespace($this->moduleNamespace)
            ->group(module_path('AutoLookup', 'Routes/api.php'));
    }

This is my Api.php route sorry about the mess I've been trying everything to get it to work. I left the commented-out routes to show you what I've tried.

//Route::get('/test/route', function(\Illuminate\Http\Request $request){
//    return  'test';
//});
//Route::middleware('api')->get('test', 'TestController@index');
//Route::get('/test2', function (Request $request) {
//    return 'ok';
//});
////Route::get('/lookups', 'ListLookupsController')->name('list');
//
//// Route::get('/lookups', '\Archix\AutoLookup\Http\Controllers\ListLookupsController@__invoke')->name('list');
Route::group(['as' => 'api.lookup.'], function () {
    // list all lookup models with url to each
    Route::get('/lookups', 'ListLookupsController')->name('list');
    // get lookup by name, the routeKey for lookup model is name...
    Route::get('/lookup/{lookup}', 'LookupController')->name('get');
});

artisan route list shows the route with or with optimize, I can see that my module is loaded everything, When I step through the code from setting a breakpoint in API routes file I go through the normal Laravel route mapping and that looks fine it maps to __invoke or If I try with a method with @method, etc. But If I set a breakpoint into the Controller then I will never get hit, Yet stepping through the code I can see the Laravel-modules file finder finds the controller file that the only time the code gets near the controller and if I continue to step through the code I get to Laravel calling render and returning some HTML which is what would happen if I did not have a route defined.

Any help would be great if it turned out to be a bug in Laravel-Module I would even be happy to fix the bug and PR. I've spent my whole Saturday stepping through the code from line 1 in index.php till Laravel render is called and I can not seem to work out what's going on I have no exceptions of any kind.

foxyntax commented 3 years ago

Same Issue

jamols09 commented 3 years ago

Does The issue still persist? I really wanted to apply modular approach but using api

apps4u commented 3 years ago

Yes I can run route:list command and it will show the routes, but they won't work I have to copy the routes into the project's main routes file for them to work which is not good as the whole point of using modules was to package everything. Its not just this modules API routes and its all modules routes they only work if I copy them to the projects main routes files yet the route list shows that they are registered

maxcelos commented 3 years ago

Please share your code. There is no way this package exists with a bug like this. I use api routes all the time without any issues.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

bci24 commented 2 years ago

I have the same issue here. Ex for the route /api/test ... if I move this line:

Route::get('/test', [TestController::class, 'index']);

from api.php (module) to api.php (main) it works

If the route api (from above) is in the api.php (module) it does not hit the TestController.

Mllexx commented 2 years ago

Having the same issue, anyone who's managed to find a workaround?

bci24 commented 2 years ago

no. I am still searching for a solution

hamzakifani commented 2 years ago

Hello , I think that help you https://stackoverflow.com/questions/58243133/laravel-modules-vuejs-routes-are-being-overwritten/58244647#58244647

srakl commented 7 months ago

same, i can't hit the api.php routes for my API module. I'm on Laravel 10

mahfuz-rahman007 commented 6 months ago

I just faced the same issue, i am using modules system for my Vue app, and the APIs don't work from the modules. And I find out that because of the wildcard entry routes, the issue is causing.

So I followed what @hamzakifani suggested and used ->where('any', '^(?!api).*$'); at the end of the wildcard routes.

Now it's fixed .