Welcome Guest, Not a member yet? Register   Sign In
How to avoid having BOTH __construct AND initController!?
#1

I'm used to __construct. Now, CI4 no longer use that BUT have 'replaced' it, it seems, with an initController method.

This is confusing, at best. And now I have to add this weird initController to get access to the request in my ResourceController ... it all has become weird somehow.

I want AAAAAALLLLLLLL my ResourceControllers to have access to the $request in the 'constructor' function (yes, otherwise it's bleedin' impossible to make anyting dynamic) ... HOW do I do that in a clear and clean way (preferably not having to have to use this weird initController thing, as it doesn't make any sense to me).

And can someone explain to me a case where a ResourceController would NOT need a $request?

Please, enlighten me, as there seems to be things I don't get about this CI4 thing. : )
Reply
#2

What do you need to do in __construct that you could not do in initController?
Reply
#3

PHP Code:
$request = \Config\Services::request(); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(12-17-2020, 04:04 PM)ojmichael Wrote: What do you need to do in __construct that you could not do in initController?

Probably nothing. But I'm having issues seeing the point of needing initController when being used to __construct. I find it counter intuitive to use (because I basically don't understand the purpose of it).

In my case I have a Controller that extends ResourceController (for an API). Now, ResourceController extends Controller, and we have BaseController (for things like initController I guess, from what I can understand reading the docs ... yeah, they're another issue). Now, WHAT would I have to do to make ResourceController extend (my) BaseController? I can't see that happening in any elegant or logic way.

What I'm basically aiming at is, that I set all these table relevant configurations dynamically on each request. That is, I pass in a resource (in the url), and then set all the database yadda yadda based on that ... and I do that in what I understand as the constructor (before anything else is called - think of it as controller wide settings). Now, I would also like to set $allowedFields dynamically in the process – so that I don't have to sprinkle that kind of code all over my controller wherever I may need it.

And it then baffles me, that I don't have access to the request in the __construct (I know it's kind of 'moved' to the initController). But that would mean that every time I need __construct like features I would have to use initController ... now, as I understand it __construct is native PHP, so I guess it's known and tested. Now, along comes CI4 and kind of 'change' that basic feature, and introduces something 'different' that seem to do basically the same thing ... and maybe I just don't get it. And if I don't, I guess a lot of others don't either ... and is that a good thing? You tell me.

And if you know something about initController that would help me understand why this is better than (the well known) __construct, please enlighten me ... I'm happy to learn.

But from this 'write' you should get an understanding of what my issue is, and maybe also what information I'm lacking.
Reply
#5

Read Kilishan’s answer in this thread: https://forum.codeigniter.com/thread-733...controller
The purpose of initController is to not have the framework’s code in the controller’s constructor. I guess it’s to prevent people from overriding the constructor and forgetting to call the parent one. But, the good news is that you can still use the constructor for your own needs. Maybe you are overthinking this. The base controller and the initController() method are extra stuff you can use, there’s nothing that prevents you from defining __contruct() in your controller.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB