CodeIgniter Forums
How to load a model in Autoload file - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: How to load a model in Autoload file (/showthread.php?tid=79033)

Pages: 1 2


How to load a model in Autoload file - ciddict - 04-11-2021

How can I load a model in ...\app\Config\Autoload.php?

PHP Code:
namespace Config;

use 
CodeIgniter\Config\AutoloadConfig;

class 
Autoload extends AutoloadConfig {

    public function __construct() {
         
//load here
         //i've tried,
         
$my_model = new \App\Models\My_model();
 
        $my_model model("App\Models\My_model");
    

Nothing is working.

What is the correct way? Looking for help.
Thanks in advance.


RE: How to load a model in Autoload file - InsiteFX - 04-11-2021

You should be able to load it using the ClassMap.

PHP Code:
$classmap = [
    'Markdown' => APPPATH 'third_party/markdown.php'
]; 



RE: How to load a model in Autoload file - ciddict - 04-11-2021

@InsiteFX, I didn't get it from your answer. Can you please be more specific?


RE: How to load a model in Autoload file - iRedds - 04-11-2021

Why push the model to autoload?
The Autoload class is a tool for defining class file paths and including. It is not for loading classes directly.


RE: How to load a model in Autoload file - ciddict - 04-13-2021

@iRedds, so there has no way to get a model in autoload class?


RE: How to load a model in Autoload file - iRedds - 04-13-2021

Аbsolutely.
Tell me why are you trying to do this?


RE: How to load a model in Autoload file - ciddict - 04-13-2021

I want to push some data to $psr4 variable dynamically. And I've to get that data from model.
How could I do that?


RE: How to load a model in Autoload file - iRedds - 04-14-2021

Yes, you can add values dynamically.
But you won't create an instance of the model.

Take a look at the event documentation.
https://codeigniter.com/user_guide/extending/events.html


RE: How to load a model in Autoload file - ciddict - 04-14-2021

@iRedds, OK. Thanks.


RE: How to load a model in Autoload file - ciddict - 04-15-2021

@iRedds, Can you please let me know how could I do that?