15

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.

jeesoon
  • 377
  • 1
  • 3
  • 15

7 Answers7

33

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

Route::get('/hello', ...)
axios.get('/api/hello')
danronmoon
  • 3,814
  • 5
  • 34
  • 56
Eliseo
  • 604
  • 4
  • 10
  • 1
    The prefixed urls were also tested, so I don't think this is the solution to this problem. – Henridv Nov 01 '19 at 16:33
  • It's right there in the question. "i tried accessing it by `/127.0.0.1:8000/api/dashboard/departments`" – miken32 Dec 13 '21 at 16:49
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.

Alec Gordon
  • 325
  • 4
  • 10
  • 1
    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 : http://localhost/mylaravelprojects/myjwtapp/public/api/user. This works. Of course, in real project, I need to implement Authentication, either with Passport or simple JWT. – Lex Soft Apr 15 '20 at 17:48
  • 1
    Nice explanation – Richard Jan 09 '21 at 20:18
5

Just add public in url before api.

Like

/127.0.0.1:8000/public/api/dashboard/departments
Paras Raiyani
  • 748
  • 5
  • 10
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');

jeesoon
  • 377
  • 1
  • 3
  • 15
  • 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. – Lex Soft Apr 15 '20 at 18:22
  • 1
    What is the point of this? If you don't want the API prefix, just put the routes in `web.php`. – miken32 Dec 13 '21 at 16:44
3

just run

php artisan route:clear
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).

  • 1
    Changing the accept header will have no bearing on whether or not you get a 404. – miken32 Feb 04 '22 at 01:44
  • 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. – Osinachi Jul 28 '23 at 14:12
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

Amina Darwish
  • 328
  • 3
  • 5