Welcome Guest, Not a member yet? Register   Sign In
CI4: How to autoload libraries like Database, Session etc
#7

(11-13-2018, 12:59 PM)webdevron Wrote: Please help me by providing a code snip for autoloading libraries like Database, Session etc.
In CI3 we use it as
PHP Code:
$autoload['libraries'] = array('user_agent''database''session'); 

What would be the equivalent of this line in CI4?

Thanks in advance.


Hi,
You can do the Same in Basecontroller now. Please follow the below example

@BaseController


public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);

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

        $this->session = \Config\Services:Confusedession();
        $this->validation =  \Config\Services::validation();
    }


Then the same can be called from all controllers which extends basecontroller

@otherControllers

$this->session->destroy();


Thanks.
Reply


Messages In This Thread
RE: CI4: How to autoload libraries like Database, Session etc - by ppuhan1389 - 06-21-2020, 12:43 AM



Theme © iAndrew 2016 - Forum software by © MyBB