Welcome Guest, Not a member yet? Register   Sign In
ErrorException Undefined variable: db
#1

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

namespace App\Controllers;

use 
CodeIgniter\Controller;
use 
CodeIgniter\HTTP\CLIRequest;
use 
CodeIgniter\HTTP\IncomingRequest;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
Psr\Log\LoggerInterface;

/**
 * Class BaseController
 *
 * BaseController provides a convenient place for loading components
 * and performing functions that are needed by all your controllers.
 * Extend this class in any new controllers:
 *    class Home extends BaseController
 *
 * For security be sure to declare any new methods as protected or private.
 */

class BaseController extends Controller
{
 
/**
 * Instance of the main Request object.
 *
 * @var IncomingRequest|CLIRequest
 */
 
protected $request;

 
/**
 * An array of helpers to be loaded automatically upon
 * class instantiation. These helpers will be available
 * to all other controllers that extend BaseController.
 *
 * @var array
 */
 
protected $helpers = [];

 
/**
 * Constructor.
 *
 * @param RequestInterface  $request
 * @param ResponseInterface $response
 * @param LoggerInterface  $logger
 */
 
public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
 {
 
// Do Not Edit This Line
 
parent::initController($request$response$logger);

 
//--------------------------------------------------------------------
 // Preload any models, libraries, etc, here.
 //--------------------------------------------------------------------
 // E.g.: $this->session = \Config\Services::session();
 
$this->session = \Config\Services::session();
 
$this->db = \Config\Database::connect(); 
 }



and this is my Controller
PHP Code:
<?php 
namespace App\Controllers;
use 
CodeIgniter\Controller;

class 
Landing extends BaseController{
 
 public function 
index(){
 
 
var_dump($db);
 echo 
'test';
 }


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.
Reply
#2

(This post was last modified: 07-04-2021, 07:23 AM by includebeer.)

$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.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

(This post was last modified: 07-04-2021, 10:33 AM by ikesela.)

db is a shared instances.

$db = db_connect()

from model class , can access thru : $this->db
Reply
#4

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




Theme © iAndrew 2016 - Forum software by © MyBB