CI4: How to autoload libraries like Database, Session etc |
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.
@webdevron,
I know that the CI 4 documentation is still a work in progress but here you go... http://codeigniter4.github.io/CodeIgnite...utoloading
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.
(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.
Have you looked at the user guide yet? It's fairly up to date, I believe.
Use '( ![]() If you have a specific question we can help.
(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: ![]() $this->session = \Config\Services: ![]() $this->validation = \Config\Services::validation(); } Then the same can be called from all controllers which extends basecontroller @otherControllers $this->session->destroy(); Thanks.
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 )
|
Welcome Guest, Not a member yet? Register Sign In |