'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.
Solution 1:[1]
Remember that routes declared in api.php will automatically prepend the /api prefix, e.g.:
Route::get('/hello', ...)
axios.get('/api/hello')
Solution 2:[2]
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.
Solution 3:[3]
Just add public in url before api.
Like
/127.0.0.1:8000/public/api/dashboard/departments
Solution 4:[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');
Solution 5:[5]
just run
php artisan route:clear
Solution 6:[6]
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
Solution 7:[7]
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).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | danronmoon |
| Solution 2 | Alec Gordon |
| Solution 3 | Paras Raiyani |
| Solution 4 | jeesoon |
| Solution 5 | Hossein Rahimi |
| Solution 6 | Amina Darwish |
| Solution 7 | Matheus Landuci |
