Reputation: 117
I'm new in larevel. I want to create route in api.php. It's my code in this file
Route::middleware('auth:api')->get('/api', function (Request $request) {
return response()->json([
'name' => 'Abigail',
'state' => 'CA'
]);
});
I need to return json but when I put url mysite.com/api/api and page redirect me to mysite.com/user. How I can avoid redirect I get correct url?
Upvotes: 0
Views: 338
Reputation: 2059
Here is a another example to parse direct model.
Route::middleware('api')->get('/api/users', function (Request $request) {
return \App\Users::all();
});
You will get a json object for all users table data.
Upvotes: 1
Reputation: 40683
You're getting redirected because you're using the auth
middleware and are not authenticated. If the route does not need authentication just do:
Route::get('/api', function (Request $request) {
return response()->json([
'name' => 'Abigail',
'state' => 'CA'
]);
});
Upvotes: 1
Reputation: 4114
Remove auth
middleware and try again like:
Route::middleware('api')->get('/api', function (Request $request) {
return response()->json([
'name' => 'Abigail',
'state' => 'CA'
]);
});
Upvotes: 2