FIXED: I have been trying programming in CodeIgniter 4. Things were going great until I hit this roadblock. I have researched thru the code, CI docs and searched online without a solution to this issue and I don’t understand why CI cannot see that file. One thing to note is that in the IDE this class is mapped correctly (IE. I can control -> click on the Class initialization call and it will take me to the correct file. I need some help as I cannot see where the problem is.
ERROR: CRITICAL Class "CompanyPostTranslatorModelsTranslateCategoryModel" not found
Application structure
- app
- assets
- modules
- Company
- PostTranslator
- Models
- TranslateCategoryModel.php
- Models
- PostTranslator
- Company
- writable
- …
This is how I have it setup:
app/Config/Autload.php (PSR4)
public $psr4 = [
APP_NAMESPACE => APPPATH, // For custom app namespace
'Config' => APPPATH . 'Config',
'CompanyPostTranslator' => ROOTPATH . 'modules/Company/PostTranslator',
];
modules/Company/PostTranslator/Models/TranslateModel.php
<?php
namespace CompanyPostTranslatorModels;
use CompanyPostTranslatorModelsTranslateCategoryModel;
use AppModelsPostAdminModel;
class TranslateModel extends PostAdminModel
{
public $translateCategoryModel;
public function __construct()
{
$this->translateCategoryModel = new TranslateCategoryModel();
}
public function addTranslatedPost($postData, $translatedContent)
{
...
}
}
modules/Company/PostTranslator/Models/TranslateCategoryModel.php
<?php
namespace CompanyPostTranslatorModels;
use AppModelsCategoryModel;
class TranslateCategoryModel extends CategoryModel
{
...
}
I don’t see what I am possibly doing wrong.
The following for example, works without issues
modules/Company/PostTranslator/Config/Events.php
<?php
namespace CompanyPostTranslatorConfig;
use CodeIgniterEventsEvents;
Events::on('pre_system', function () {
log_message('error', "FIRING EVENT pre_system");
});
LOG FILE ENTRY
ERROR - 2023-02-13 10:48:16 --> FIRING EVENT pre_system
2
Answers
The file's extension was removed somehow. IDE did not detect the problem and neither did I. I only was able to figure out after I tried require. I am mad!
You should run
composer dump-autoload
EDIT:
The key of each row is the namespace itself. This does not need a trailing back slash. The value is the location to the directory the classes can be found in. They should have a trailing slash. Codeigniter Documentation
So you have to change the
$psr4
like this:Possible explanation why the The following for example, works without issues
modules/Company/PostTranslator/Config/Events.php works without issues is because this is not a Class, so even if you add namespaces above it, doesn’t affect at all (you can try to remove namespace for the Config).