Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel API Routes Not Found

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.

like image 994
jeesoon Avatar asked Mar 08 '19 03:03

jeesoon


3 Answers

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

Route::get('/hello', ...)
axios.get('/api/hello')
like image 107
Eliseo Avatar answered Nov 08 '22 14:11

Eliseo


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.

like image 26
Alec Gordon Avatar answered Nov 08 '22 14:11

Alec Gordon


Just add public in url before api.

Like

/127.0.0.1:8000/public/api/dashboard/departments
like image 33
Paras Raiyani Avatar answered Nov 08 '22 14:11

Paras Raiyani