25

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.

5
  • Only one route for a Route::resource ? Commented Mar 8, 2019 at 3:11
  • 2
    please, enter this command (php artisan route:clear) And then run the server again (php artisan serve) Commented Mar 8, 2019 at 3:13
  • there are more routes, i just show the index and i already run the route:clear Commented Mar 8, 2019 at 3:15
  • Does the root page show? 127.0.0.1:8000/ Commented Mar 8, 2019 at 3:21
  • yes the root show Commented Mar 8, 2019 at 3:24

9 Answers 9

48

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

Route::get('/hello', ...)
axios.get('/api/hello')
Sign up to request clarification or add additional context in comments.

2 Comments

The prefixed urls were also tested, so I don't think this is the solution to this problem.
It's right there in the question. "i tried accessing it by /127.0.0.1:8000/api/dashboard/departments"
32

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.

3 Comments

This answer was helpful for my similar error
This is the right answer!
it help me, even ai tool were not able to slove this great job
9

just run

php artisan route:clear

1 Comment

Thanks, It works, anything related to why we need a clear to make it effect?
8

Just add public in url before api.

Like

/127.0.0.1:8000/public/api/dashboard/departments

8 Comments

@jeesoon Show your complete URL that you are trying.
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/departments
Add public keyword before api like this url----> /127.0.0.1:8000/public/api/dashboard/departments
And why would he do that? There's no "public" prefix specified anywhere in the code.
It's not in Laravel by default unless your web server is misconfigured.
|
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.

2 Comments

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.
Nice explanation
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');

2 Comments

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.
What is the point of this? If you don't want the API prefix, just put the routes in web.php.
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).

2 Comments

Changing the accept header will have no bearing on whether or not you get a 404.
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.
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

Comments

0

Add this in your api.php file.

Route::group([
    'prefix' => 'api',
    'as' => 'api.'
    'middleware' => 'api'
 ], function(){
    Route::get('/dashboard/departments', 'DepartmentsController@index')->name('index');
 });

or you could do

Route::prefix('api')->name('api.')->middleware('api')->group(function () {
    Route::get('/dashboard/departments', 'DepartmentsController@index')->name('index');
});

both work the same way.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.