skip to Main Content

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
  • 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


  1. Chosen as BEST ANSWER

    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!


  2. 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:

    public $psr4 = [
        APP_NAMESPACE => APPPATH, // For custom app namespace
        'CompanyPostTranslator' => ROOTPATH . 'modules/Company/PostTranslator/',
    ];
    

    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).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search