Wednesday, March 15, 2017

Laravel Kernel middlewareGroups works for the group routes itself but not for group route inside the group

Sorry for the weird title. I'm not a native speaker. So, here is my routes/api.php content:

Route::post('login', 'Api\LoginController@login');

Route::group([
    'prefix' => 'restricted',
    'middleware' => 'auth:api',
], function () {
    // Authentication Routes...
    Route::post('logout', 'Api\LoginController@logout');

    Route::get('user', 'Api\LoginController@getUser');
});

And as you probably know, every route inside this file has the api prefix by default. In my Kernel.php I've set a middleware for api routes:

protected $middlewareGroups = [
    'web' => [
        ...
    ],

    'api' => [
        ...,
        \Barryvdh\Cors\HandleCors::class,
    ],
];

Now my problem is that when i hit the myapp/api/login from a seprated app (angular.js app) it works fine but when i try to fetch user data from myapp/api/restricted/user i get a No 'Access-Control-Allow-Origin' header is present on the requested resource. error.

How can i fix this?



via Jhivan

Advertisement