7
votes

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.

4
Only one route for a Route::resource ?keyboardSmasher
please, enter this command (php artisan route:clear) And then run the server again (php artisan serve)Igor Carvalho
there are more routes, i just show the index and i already run the route:clearjeesoon
Does the root page show? 127.0.0.1:8000/keyboardSmasher
yes the root showjeesoon

4 Answers

20
votes

remember than routes declared in api.php will automatically concat to /api prefix, btw:

Route::get('/hello', ...)
axios.get('/api/hello')
5
votes

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.

5
votes

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');

3
votes

Just add public in url before api.

Like

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