Lamar
Lamar

Reputation: 1849

Using api route without authentication in Laravel

I want to simply return some JSON data from a controller, therefore I thought to use api route.

MyController.php:

public function data()
{
    //return json string
}

That controller is supposed to be used by both api and web routes, depending on my needs.

api.php:

Route::get('data', 'MyController@data');

When I visit the path: /api/data it redirects me to /home.

However, when I change my route to the following it works:

Route::get('data', function() {
    //return json string
});

I am not sure if it is related to authentication. I am not binding any middleware to my route as you can see.

What seems to be the problem here?

Upvotes: 3

Views: 2665

Answers (1)

Ali Raza
Ali Raza

Reputation: 192

If there's a middleware defined in your controller, do this

$this->middleware('auth', ['except' => ['yourMethodName']]);

Upvotes: 4

Related Questions