Search code examples
laravel-controller

Laravel custom controller not found when changing the controller directory


When changing the controller directory location like so:

Route::group([
  'prefix'      => 'report',
  'middleware'  => 'auth',
], function() {
    Route::get('/summary','IOS\ReportController@index');
});

It returns an error message:

Error
Class 'App\Http\Controllers\IOS\Controller' not found

But it's working perfectly the below way:

Route::group([
  'prefix'      => 'report',
  'middleware'  => 'auth',
], function() {
    Route::get('/summary','ReportController@index');
});

After changing directory location, i try to composer dump-autoload but it's still getting error.


Solution

  • every Controller on laravel should extends the base laravel Controller
    the base controller is in this path: App\Http\Controllers
    so when you create a controller on another folder, the created controller wants to extend from the base controller but can not find it in folder
    so you should do this on ReportController

    namespace App\Http\Controllers\IOS;   //namespace of your controller
    use App\Http\Controllers\Controller;  //the path of base Controller
    
    class ReportController extends Controller  //your controller extends from base controller