Welcome Guest, Not a member yet? Register   Sign In
Autoload for models like BaseController
#4

Unlike CI3, version 4 has convenient ways for accessing components on-the-fly. There is nothing wrong with preloading instances into your BaseController but a) you run the risk offloading things you never use for a performance hit, and b) these instances would need to be passed from your Controller if you want them accessible anywhere else.

For starters I recommend reading up on Services. This is CI4’s version of a dependency container - a way of making “shared instances” of various components available to other components. So I’m your example, instead of preloading a URI instance you can just grab it when you need it:

service('uri')->setPath('foo/bar');


Because Services is managing your instances, this will be the same object anywhere you call it. So if the above code was in a Filter then you can reuse the same URI in your view:

Current path is <?= service('uri')->getPath() ?>
Reply


Messages In This Thread
RE: Autoload for models like BaseController - by MGatner - 06-22-2021, 05:22 AM



Theme © iAndrew 2016 - Forum software by © MyBB