• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CI4: How to autoload libraries like Database, Session etc

#1
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.
"Who thinks in code"
Perfectly describes who I am
mbparvez.me
Reply

#2
@webdevron,

I know that the CI 4 documentation is still a work in progress but here you go... http://codeigniter4.github.io/CodeIgnite...utoloading
Reply

#3
it depend on the libraries
session is a service now and can be accessed by
Code:
\Config\Services::session()

to get a database connection
Code:
\Config\Database::connect()
Reply

#4
Autoloading it like you're thinking of from CI3 isn't really a thing anymore, but it's simple to handle yourself. If you find that you need the same libs loaded every time, you can make a BaseController that all of your other controllers extend from. And you can load it from there.

Many times, though, you can simple get the class through Services, as puschie pointed out, whenever you need it. Session doesn't need to be loaded before you need it, though, as the first time it is used it will automatically be initialized.
Reply

#5
(11-13-2018, 03:42 PM)php_rocs Wrote: @webdevron,

I know that the CI 4 documentation is still a work in progress but here you go... http://codeigniter4.github.io/CodeIgnite...utoloading

Not at all the same thing. Easy to confuse due to the terminology, but the page you reference is about locating and using files when they are requested. It is not a "list of requested classes to automatically load at a certain time" which is what the OP is looking for.
Reply

#6
Have you looked at the user guide yet? It's fairly up to date, I believe.

Use '(Confusedegment)' in place of '(:any)' reference the user guide. The changes in routing are fairly drastic but very powerful.

If you have a specific question we can help.
Reply

#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

#8
Next time please place your code in side of code tags.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.