CodeIgniter Forums
CI4: How to autoload libraries like Database, Session etc - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: CI4: How to autoload libraries like Database, Session etc (/thread-72170.html)



CI4: How to autoload libraries like Database, Session etc - webdevron - 11-13-2018

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.


RE: CI4: How to autoload libraries like Database, Session etc - php_rocs - 11-13-2018

@webdevron,

I know that the CI 4 documentation is still a work in progress but here you go... http://codeigniter4.github.io/CodeIgniter4/concepts/autoloader.html?highlight=autoloading


RE: CI4: How to autoload libraries like Database, Session etc - puschie - 11-14-2018

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



RE: CI4: How to autoload libraries like Database, Session etc - kilishan - 11-14-2018

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.


RE: CI4: How to autoload libraries like Database, Session etc - dave friend - 11-14-2018

(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/CodeIgniter4/concepts/autoloader.html?highlight=autoloading

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.


RE: CI4: How to autoload libraries like Database, Session etc - kilishan - 11-20-2018

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.