CI4: How to autoload libraries like Database, Session etc - 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: CI4: How to autoload libraries like Database, Session etc (/showthread.php?tid=72170) |
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, 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 '(egment)' 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. RE: CI4: How to autoload libraries like Database, Session etc - ppuhan1389 - 06-21-2020 (11-13-2018, 12:59 PM)webdevron Wrote: Please help me by providing a code snip for autoloading libraries like Database, Session etc. 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:ession(); $this->session = \Config\Services:ession(); $this->validation = \Config\Services::validation(); } Then the same can be called from all controllers which extends basecontroller @otherControllers $this->session->destroy(); Thanks. RE: CI4: How to autoload libraries like Database, Session etc - InsiteFX - 06-21-2020 Next time please place your code in side of code tags. |