Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ChrisR's avatar

404 accessing api <Solved>

Hi there. I am using laravel 8 [using backpack admin] and have a working app locally and in production. I just added a modal which gets data using an API.

When using php artisan serve the API works but if I access localhost with xampp or over apache on my server it does not work.

In my routes/backpack/custom.php I have this

<?php


use App\Http\Controllers\Admin\CenterSwitchController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\ReconciliationTransactionController;

Route::group([
    'prefix'     => config('backpack.base.route_prefix', 'admin'),
    'middleware' => array_merge(
        (array) config('backpack.base.web_middleware', 'web'),
        (array) config('backpack.base.middleware_key', 'admin'),
        // (array) config('backpack.base.middleware_class')
    ),
    'namespace'  => 'App\Http\Controllers\Admin',
], function () { // custom admin routes
    Route::crud('centre', 'CentreCrudController');
	
	...
	
    Route::group([
        'prefix'     =>  'api',
        'middleware' => array_merge(
            (array) config('backpack.base.web_middleware', 'web'),
            (array) config('backpack.base.middleware_key', 'admin')
        ),
        'namespace'  => 'App\Http\Controllers\Api',
    ], function () {
        Route::get('students', 'StudentController@index');
    });

}); 

And the code can be found under app/http/controller/api/studentController.php

Is the route in custom.php written incorrectly? Or does the problem lie somewhere else?

Thanks in advance

0 likes
2 replies
ChrisR's avatar

Adding some additional information ...

My routes/api.php does not have anything related as it is in routes/backback/custom.php as my above post


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;


Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

My api code is found in /App/Http/Controllers/Api/StudentController.php

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Student;

class StudentController extends Controller
{
    public function index(Request $request) {
...

If I run artisan route:list I get target class [App\Http\Controlllers\Admin\Http\Controllers\Api\StudentControllre] does not exist

If I edit my routes/backpack/custom.php to

<?php


use App\Http\Controllers\Admin\CenterSwitchController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\ReconciliationTransactionController;

Route::group([
    'prefix'     => config('backpack.base.route_prefix', 'admin'),
    'middleware' => array_merge(
        (array) config('backpack.base.web_middleware', 'web'),
        (array) config('backpack.base.middleware_key', 'admin'),
        // (array) config('backpack.base.middleware_class')
    ),
    'namespace'  => 'App\Http\Controllers\Admin',
], function () { // custom admin routes
    Route::crud('centre', 'CentreCrudController');
	
	...
	
}); 

    Route::group([
        'prefix'     =>  'api',
        'middleware' => array_merge(
            (array) config('backpack.base.web_middleware', 'web'),
            (array) config('backpack.base.middleware_key', 'admin')
        ),
        'namespace'  => 'App\Http\Controllers\Api',
    ], function () {
        Route::get('students', 'StudentController@index');
    });

There will be no more errors displayed when I run artisan route:list but the 404 still exists and the url is still http://localhost/api/students?search=eee&page=1

ChrisR's avatar

Managed to solve it with the help of https://github.com/o15a3d4l11s2

Needed to use {{ route('students.index') }} for the url and in my route file, custom.php, define the route like this Route::get('students', 'StudentController@index')->name('api.students');

Please or to participate in this conversation.