Route api in Laravel 5.4

Asked

Viewed 702 times

0

Through Composer, I started a project with the command:

composer create-project --prefer-dist laravel/laravel api

After the installation of the dependencies, I was able to visualize the "Welcome" of the application (Through the standard wheel that is already created at the time of project startup).

In the wheel file, inside the directory route the file web.php, there is the code:

Route::get('/', function () {
    return view('welcome');
});

So far so good, that even though the application was created correctly, only that I have no interest in making the web application by the Laravel, I just want to use the api Laravel, for this:

Inside the directory route in the archive api.php:

Route::get('/', function () {
     return 'API';
});

The paths to access the api should remain, however regardless of the route I configure in this file (api.php) is not processed by the application, the following error appears when I access the link http://localhost/api/public/api:

Erro ao tentar acessar as rotas de api da aplicação

What I might have done wrong, because I can’t access the api routes?

  • try only localhost/api. No need to place localhost/<project>/public name to access your endpoints (whereas you have set Laravel correctly to be the webserver root)

  • @gmsantos did not configure anything, I think just put the routes in the api.php file and everything will work. To answer you: it doesn’t work, I try in life that I have several projects within the localhost, so this Laravel project is just another folder. From what you said I think I need to set something up..

  • Updates to the Laravel 5.6 that goes perfect.

  • I updated and the error persisted, I deleted the project and ran the command again, it worked! I think there may have been some problem at the time the script was running.

1 answer

0

Open the terminal (mac) or cmd prompt (windows), navigate to the project folder and type the following.

php artisan route:list

The URI can be used for existing routes.

Watch out for Routeserviceprovider. API routes have a prefix 'api'. The way you are calling I think you must have put the project inside a folder called api.

  • I had already listed the wheels, and even with this help did not work, I ended up deleting the design and remade from scratch and it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.