CodeIgniter Forums
How to connect the startup database library - 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: How to connect the startup database library (/showthread.php?tid=67270)



How to connect the startup database library - evgeny - 02-02-2017

Hello.

How to connect the startup database library in file autoload.php


RE: How to connect the startup database library - Wouter60 - 02-02-2017

PHP Code:
$autoload['libraries'] = array('database'); 
Besides that, you must set the correct values in application/config/database.php.
After you've done that, you're ready to go!


RE: How to connect the startup database library - kilishan - 02-02-2017

In CI4, you don't autoload things in the same way as previous versions, since we now have a PSR4-compliant autoloader built in. As per the docs, whenever you need a database connection, you simply grab it:

Code:
$db = \Config\Database::connect();

You can do that in your controller constructor, if you like to have it available for every method. Or in a BaseController that your controllers extend from.


RE: How to connect the startup database library - Wouter60 - 02-02-2017

I'm sorry. I overlooked that it was in the CodeIgniter 4 Support forum. Blush


RE: How to connect the startup database library - kilishan - 02-03-2017

(02-02-2017, 11:30 PM)Wouter60 Wrote: I'm sorry. I overlooked that it was in the CodeIgniter 4 Support forum. Blush

No worries. Smile


RE: How to connect the startup database library - evgeny - 02-24-2017

Thank you


RE: How to connect the startup database library - zarbux - 03-19-2017

(02-02-2017, 09:42 PM)kilishan Wrote: In CI4, you don't autoload things in the same way as previous versions, since we now have a PSR4-compliant autoloader built in. As per the docs, whenever you need a database connection, you simply grab it:

Code:
$db = \Config\Database::connect();

You can do that in your controller constructor, if you like to have it available for every method. Or in a BaseController that your controllers extend from.

Dear all,
I use a lot CI , looking now to CI4 ... I clearly understand I can connect by adding this in the controller constructor. But I want  to connect db only one time for my whole app. Somewhere like a general autoload for the connect db
if Someone can tell me where I can do that like in the "old" CI.
Thanks by advance.