What are the CI4 ways to use Database and Library in a Helper? |
I need to use a database model and library in my helper. Also need to use a library. In CI 3 I used to do it like following:
PHP Code: $CI =& get_instance(); Now I am little bit confused about the CI 4. What are the ways to do this? What is the CodeIgniter way to do it? Thanks in advance.
(08-15-2019, 10:21 AM)webdevron Wrote: I need to use a database model and library in my helper. Also need to use a library. In CI 3 I used to do it like following: I answered you here, where you previously asked this question. (08-15-2019, 10:34 AM)dave friend Wrote:(08-15-2019, 10:21 AM)webdevron Wrote: I need to use a database model and library in my helper. Also need to use a library. In CI 3 I used to do it like following: Thanks for your response in both threads. It was 15 minutes before my post. Currently I'm solving the issue like bellow. PHP Code: // -------------------------- BaseController Constructor Need your feedback about this solutions. Again I am little bit confused about Service. I have read through the docs but still not clear about understanding "What is service?".
A "Service" is just a function that takes care of initializing an object with any dependencies it needs, and returning it. By default it returns the same instance every time that it's called so it's shared across the application, but you can request a new instance easily.
As for your code example - I wouldn't pass a controller into a helper function. Just pass what it needs, which is likely just the model at that point, since the model contains it's own pointer to the database. PHP Code: // -------------------------- Function in BaseController
(08-15-2019, 11:41 AM)webdevron Wrote: Again I am little bit confused about Service. I have read through the docs but still not clear about understanding "What is service?". In CIv4 a Service is a type of factory to create new instances of the desired class. In other words, think of it as a kind of "helper" to load and instantiate a class. If you haven't already, read the v4 Documentation - Services info here (08-15-2019, 12:05 PM)kilishan Wrote: A "Service" is just a function that takes care of initializing an object with any dependencies it needs, and returning it. By default it returns the same instance every time that it's called so it's shared across the application, but you can request a new instance easily. Thank you kilishan. I just need to know that, My process is not bad and you make me feel that. (08-15-2019, 12:08 PM)dave friend Wrote:(08-15-2019, 11:41 AM)webdevron Wrote: Again I am little bit confused about Service. I have read through the docs but still not clear about understanding "What is service?". Think of it as a kind of "helper" to load and instantiate a class - this is very much easy to understand. Do not know why, CI4 doc is a little bit harder to understand than CI2 and CI3. But people like you, always make this easy. Thank you again. |
Welcome Guest, Not a member yet? Register Sign In |