I’m developing an e-commerce website using Laravel 8, And I tried to change on Laravel folders structure I’ve put views folder inside new path modules/admins which works fine, and I’ve also created a Models folder in the modules/admins path which works fine, however, I am having trouble with the controllers folder.
I still have the app/http/controllers/Controller.php, but now I’ve created a folder called Controllers inside the modules folder, and in it I have a controller called AdminController
error message: “Target class [ModulesAdminsControllersAdminController] does not exist.”
project structure enter image description here
AdminController file
namespace ModulesAdminsControllers;
use AppHttpControllersBaseController;
use AppHttpTraitsApiDesignTrait;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use IlluminateSupportFacades{
Hash, Validator
};
class AdminController extends BaseController
{
public function test()
{
return true;
}
}
route.php
use IlluminateSupportFacadesRoute;
use ModulesAdminsControllersAdminController;
Route::post('/test', [AdminController::class, 'test']);
BaseController.php
namespace AppHttpControllers;
use AppHttpControllersController;
class BaseController extends Controller
{
}
Controller.php
namespace AppHttpControllers;
use IlluminateFoundationAuthAccessAuthorizesRequests;
use IlluminateFoundationBusDispatchesJobs;
use IlluminateFoundationValidationValidatesRequests;
use IlluminateRoutingController as BaseController;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
Advertisement
Answer
Because your defined namespace is outside of app, you should autoload it in your composer.json
file.
"autoload": {
"psr-4": {
"App\": "app/",
"Database\Factories\": "database/factories/",
"Database\Seeders\": "database/seeders/",
"Modules\": "Modules/"
}
},
Then run composer dump-autoload
and it should work fine.