Skip to content
Advertisement

how to create a contruct() in codeigniter4 controller?

class Home extends BaseController
{
    var $cache;

    public function __contruct()
    {
        parent::__construct();
        $this->cache = ConfigServices::cache();
    }
    public function reset()
    {
        $this->cache->clean();
    }
}

assume i have create a web cache.

the idea is i dont want to keep assign the same $cache in every method.

So contructor should do the job.

but when i load the page, it shows -> Undefined variable: cache

how to fix my code/this issue?

Advertisement

Answer

looks like codeigniter have already created a constructor method in the BaseController.

Therefore, i just load the function that i wish to preload/initialized in that BaseController.php.

Fixed.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement