简体   繁体   中英

Laravel API Routes using web middleware are not found

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

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM