Laravel API Routes Not Found
Asked Answered
A

8

20

Im new to API and Vue. Im working on Laravel 5.8 api.php and controllers and views and it only return 404 Not Found.

this is what ive tried

api.php

 Route::group(['middleware' => 'api'], function(){
    Route::resource('/dashboard/departments', 'DepartmentsController');
 });

Controller

class DepartmentsController extends Controller
{     
   public function index()
  {
  return 'hey';
  }
}

Route List

 GET|HEAD  | api/dashboard/departments                   | departments.index   | App\Http\Controllers\DepartmentsController@index                       | api,auth  

i tried accessing it by /127.0.0.1:8000/api/dashboard/departments and /127.0.0.1:8000/dashboard/departments but both is not working.

Arva answered 8/3, 2019 at 3:2 Comment(5)
Only one route for a Route::resource ?Intestate
please, enter this command (php artisan route:clear) And then run the server again (php artisan serve)Heredes
there are more routes, i just show the index and i already run the route:clearArva
Does the root page show? 127.0.0.1:8000/Intestate
yes the root showArva
M
45

Remember that routes declared in api.php will automatically prepend the /api prefix, e.g.:

Route::get('/hello', ...)
axios.get('/api/hello')
Minimum answered 1/11, 2019 at 16:5 Comment(2)
The prefixed urls were also tested, so I don't think this is the solution to this problem.Breeder
It's right there in the question. "i tried accessing it by /127.0.0.1:8000/api/dashboard/departments"Unwish
H
7

Your API routes are within the api middleware which requires authentication of type API. If you check out the API Authentication documentation you need to have API tokens set up and passed in with your request.

You either need to pass the token in with your request, remove the api middleware and have your API routes be unauthenticated, or move the routes that you need to access via browser out of the api middleware and into the web middleware and routes file.

Hardfavored answered 8/3, 2019 at 13:23 Comment(2)
You are right. For simple testing to verify if my API works, i.e without implementing any authentications, I follow your suggestion : I simply remove the api middleware for certain API routes so that they are unauthenticated. Hence I can access the API via url like so : localhost/mylaravelprojects/myjwtapp/public/api/user. This works. Of course, in real project, I need to implement Authentication, either with Passport or simple JWT.Grillo
Nice explanationTapley
Z
7

just run

php artisan route:clear
Zwiebel answered 10/12, 2021 at 20:31 Comment(1)
Thanks, It works, anything related to why we need a clear to make it effect?Agency
D
6

Just add public in url before api.

Like

/127.0.0.1:8000/public/api/dashboard/departments
Deodar answered 8/3, 2019 at 4:10 Comment(7)
@Arva Show your complete URL that you are trying.Businessman
i tried this suggestion /127.0.0.1:8000/public/api/dashboard/departments and the two other /127.0.0.1:8000/api/dashboard/departments and /127.0.0.1:8000/dashboard/departmentsArva
Add public keyword before api like this url----> /127.0.0.1:8000/public/api/dashboard/departmentsDeodar
And why would he do that? There's no "public" prefix specified anywhere in the code.Banky
It's laravel by default.Deodar
but you can remove public keyword in url some minor change in laravel structureDeodar
It's not in Laravel by default unless your web server is misconfigured.Unwish
A
4

For anyone still wondering, or it just me. This is what i did after many trials.

i remove the route::group from my API.php and the prefix('api') from RouteServiceProvider.php and replace it with middleware('web')

this is my RouteServiceProvider.php file

protected function mapApiRoutes()
{
    Route::middleware('api')
        ->middleware('web')
        ->namespace($this->namespace)
        ->group(base_path('routes/api.php'));
}

and this is my api.php file

Route::resource('/dashboard/departments', 'DepartmentsController');

Arva answered 8/3, 2019 at 13:52 Comment(2)
How about trying the following instead ? Don't change the content of RouteServiceProvider, so your route will still have a API prefix, in case your other API routes will need authentication. Only change your api.php file to become like you mentioned, so your certain routes like /dashboard/departments are not authenticated. Then you access the API with this url : 127.0.0.1:8000/public/api/dashboard/departments. It should work like mine. But if it doesn't, you can revert to your above solution.Grillo
What is the point of this? If you don't want the API prefix, just put the routes in web.php.Unwish
M
3

If you are using a REST Client (Imsomnia, Postman) you need to check if you are accepting a JSON response. Place a header in the request named "Accept" and "application/json" as value.

Header "Accept" with the "application/json" value

If the requested route has validations (and you are not sending the header), your response will be the root page (or a 404 Error if you don't have the route in routes/web.php).

Morly answered 4/2, 2022 at 1:24 Comment(2)
Changing the accept header will have no bearing on whether or not you get a 404.Unwish
This worked for me though the issue wasn't 404. The issue was that the requests to the API endpoint were being redirected to the root endpoint.Brockman
S
2

For latest laravel version (11.19) make sure you have api: __DIR__.'/../routes/api.php', parameter in bootstrap/app.php

<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        //
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

It started working for me just after I executed php artisan install:api which is adding some migration, packages and also modifying this file.

Scaly answered 31/7 at 13:58 Comment(1)
It helps me. thanks !!Alleris
F
0

To run laravel project "replace the host with yours" :

php artisan serve --host 10.11.222.33 --port 8000

or possible also this way

php artisan serve --host 127.0.0.1 --port 8000

and then call the API

http://10.11.222.33:8000/api/departments

Franky answered 27/10, 2021 at 6:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.