Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

besrabasant's avatar

get request to laravel api route not working in custom package

I am working with laravel 5.4 to on a custom package. I have defined my routes on a seperate file routes.php. I am loading the routes file through a service provider.

The problem I am facing is that when I send POST requests to the routes the routes are working fine, but when I send GET requests, it redirects me to the login page.

Route::group([
    'prefix' => 'api', 
    'middleware' => 'auth:api',
    function(){

        Route::get('/users',function() {
            $users = \App\User::all();    
            return response()->json($users);
        });

        Route::post('/users',function() {
                $users = \App\User::all();    
                return response()->json($users);
            });

    });

I am using POSTMAN to test my routes.

Please help.

0 likes
3 replies
KenoKokoro's avatar
Level 13

What do you have in your api middleware? Also how is your routes.php file included?

Can you paste the RouteServiceProvider where the routes.php is registered, and the api middleware as well ?

besrabasant's avatar

This is the serviceprovider I have written.

class UserServiceProvider extends ServiceProvider
{


    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
        $this->loadRoutesFrom(__DIR__.'/routes.php');
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

}

And sorry for my mistake, its the auth:api middleware

besrabasant's avatar

Anyways, I got it solved by declaring my service provider above

App\Providers\RouteServiceProvider::class,

in config\app.php file.

Previously, I had defined the serviceprovider below the App\Providers\RouteServiceProvider::class.

The fact i missed was that the routes defined in package route files are overridden by the routes defined in Laravel's route files.

1 like

Please or to participate in this conversation.