1
votes

I'm trying to setup a simple web api to update some information on my database.

When trying to go to 127.0.0.1/api/login for example it gives a 404 not found.

This is for a new project using Laravel Installer 2.1.0. I've tried setting the middleware to 'web' in the RouteServiceProvider.php

This is my routes\api.php:

Route::get('/login', function (Request $request) {
    return "login";
});

Route::get('/register', function (Request $request) {
    return "register";
});

When trying to go to localhost/api/login it gives a 404 instead of "login" When I do php artisan route:list I get:

+--------+----------+--------------+-------+---------------------------------------------------+------------+
| Domain | Method   | URI          | Name  | Action                                            | Middleware |
+--------+----------+--------------+-------+---------------------------------------------------+------------+
|        | GET|HEAD | /            |       | App\Http\Controllers\PagesController@Index        | web,auth   |
|        | GET|HEAD | account      |       | App\Http\Controllers\PagesController@Account      | web,auth   |
|        | GET|HEAD | api/command  |       | Closure                                           | web        |
|        | GET|HEAD | api/login    |       | Closure                                           | web        |
|        | GET|HEAD | api/register |       | Closure                                           | web        |
|        | GET|HEAD | builder      |       | App\Http\Controllers\PagesController@Builder      | web,auth   |
|        | GET|HEAD | login        | login | App\Http\Controllers\PagesController@Login        | web        |
|        | GET|HEAD | logout       |       | App\Http\Controllers\LoginController@Logout       | web        |
|        | POST     | signin       |       | App\Http\Controllers\LoginController@Authenticate | web        |
|        | GET|HEAD | signin       |       | Closure                                           | web        |
+--------+----------+--------------+-------+---------------------------------------------------+------------+

This is my vhost on apache:

<VirtualHost 127.0.0.1:80>
    DocumentRoot "E:\Xamp\htdocs\OnyxDashboard\public"
    DirectoryIndex index.php      
    <Directory "E:\Xamp\htdocs\OnyxDashboard\public">
        Options All
        AllowOverride All
        Order Allow,Deny
        Allow from all
    </Directory>
</VirtualHost>

Edit: I use laravel on Windows and start it by starting my XAMPP webserver Edit 2: Added the apache vhost config

1
How do you start the application? Through artisan, Apache, PHP dev server...? - LLJ97
I've added a virtualhost to my XAMPP apache-vhost.conf to point to the /public directory - Uccino _
Are you on the right port? Could you post the virtual host config too? Just so that everything is there because these things here look fine. - LLJ97
Make sure you are using the right port. Default port is 8000. Also, try with localhost:8000/api/login - Julian Mendez
Added the virtual host config I use. I should add, I've got a few views that I am able to visit. such as the login / index page. the only thing that is not working ( as of now ) is the API routes - Uccino _

1 Answers

0
votes

I believe the issue is with the request you are making. You need to try using something like Postman or cURL and make sure to have in your headers

Accept: application/json

The problem is your browser will not do this by default. Try running the following from your terminal to see if it is successful:

curl -X GET \
  http://127.0.0.1/api/login \
  -H 'Accept: application/json'