ErrorException Undefined variable: db |
I have been using CodeIgniter since version 2 and been using version 3 until this day. I would like to try CodeIgniter 4 but I can't seem to load the database library, even if the database is defined on my BaseController:
Here's the BaseController PHP Code: <?php and this is my Controller PHP Code: <?php Whenever I try to go to Landing controller it gives ErrorException that $db is undefined. But if I declare the $db = \Config\Database::connect(); on my Landing controller it works. I just want to make it global.
$db doesn't exists, use $this->db to access the variable from your base controller.
Also, don't try to use CI4 the "CI3 way". There's no more global object linking to everything like there was in CI3. The model class already have a reference to the db object, so there's no need to load it in the base controller and keep a reference in memory.
If you use the model the db connection is already made.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |