Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

336
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda