Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

335
Views
Laravel 5.4 : Api route list

I have the following lines in my routes/api.php

Route::middleware('api')->get('/posts', function (Request $request) {
    Route::resource('posts','ApiControllers\PostsApiController');
});

When I hit http://localhost:8000/api/posts it comes back blank, but when I move the above route to routes/web.php like so:

Route::group(['prefix' => 'api/v1'],function(){
    Route::resource('posts','ApiControllers\PostsApiController');
});

it works.

As a reminder I have cleared the routes cache file with php artisan route:clear and my route list comes with php artisan route:list when my routes/web.php is empty and routes/api.php has the above route:

Domain Method URI Name Action Middleware
GET|HEAD api/posts Closure api

Note that with web routes part the list comes ok and works fine.

What am I doing wrong here?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Dont use the middleware api and see following route example for API routes

Example 1 (in your api.php)

Route::get('test',function(){
    return response([1,2,3,4],200);   
});

visit this route as

localhost/api/test

Example 2 (if you want api authentication, token based auth using laravel passport)

Route::get('user', function (Request $request) {
    ///// controller
})->middleware('auth:api');

You can make get request for this route but you need to pass the access token because auth:api middleware has been used.

Note: see /app/http/kernel.php and you can find the

protected $routeMiddleware = [
//available route middlewares
]

There must not be such (api) kind of middle ware in this file (kernel.php) for routes unless you create one, that why you can not use middleware as api.

Here, How I am creating REST APIs (api.php)

//All routes goes outside of this route group which does not require authentication
Route::get('test',function(){
    return response([1,2,3,4],200);

});
//following Which require authentication ................
Route::group(['prefix' => 'v1', 'middleware' => 'auth:api'], function(){
    Route::get('user-list',"Api\ApiController@getUserList");
    Route::post('send-fax', [
        'uses'=>'api\ApiController@sendFax',
        'as'=>'send-fax'
    ]);
    Route::post('user/change-password', [
        'uses'=>'api\ApiController@changePassword',
        'as'=>'user/change-password'
    ]);

});
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!