CodeIgniter Forums
ErrorException Undefined variable: db - 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: ErrorException Undefined variable: db (/showthread.php?tid=79573)



ErrorException Undefined variable: db - lordmight - 07-04-2021

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.


RE: ErrorException Undefined variable: db - includebeer - 07-04-2021

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


RE: ErrorException Undefined variable: db - ikesela - 07-04-2021

db is a shared instances.

$db = db_connect()

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


RE: ErrorException Undefined variable: db - InsiteFX - 07-05-2021

If you use the model the db connection is already made.