Reputation: 467
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.
Upvotes: 23
Views: 55130
Reputation: 44
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.
Upvotes: 0
Reputation: 1237
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.
Upvotes: 17
Reputation: 712
Remember that routes declared in api.php
will automatically prepend the /api
prefix, e.g.:
Route::get('/hello', ...)
axios.get('/api/hello')
Upvotes: 46
Reputation: 171
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).
Upvotes: 3
Reputation: 466
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
Upvotes: 0
Reputation: 467
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');
Upvotes: 4
Reputation: 325
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.
Upvotes: 7
Reputation: 768
Just add public in url before api.
Like
/127.0.0.1:8000/public/api/dashboard/departments
Upvotes: 7