Welcome Guest, Not a member yet? Register   Sign In
Custom controller - request object empty in constructor
#5

(06-24-2022, 02:56 PM)kenjis Wrote:
(06-22-2022, 11:41 AM)groovebird Wrote: This means if i want to use the __construct i have to use \Config\Services::request().
In the meantime i used the initController in my custom controller and all the code from the constructor must be in the initController function but your solution is much better.

It is not good practice. Because CI Controller is designed to be set the request object in initController().
You are against it, and force to get the request object before CI injects it into the controller.

I recommend you not use the tricky way.

See the documentation. Using initController() is a normal way.
https://codeigniter4.github.io/CodeIgnit...components

I do not agree with this statement.
The initController() method is used because CI doesn't use a service container and can't inject dependencies.

In my opinion, the controller should not be dependent on other classes by default, and CodeIgniter\Controller and App\Controllers\BaseController should only add additional functionality/helper methods.

Since Services::request() returns the same class instance as set in $this->request, I don't see any bad practice in using a factory service in a constructor.
Reply


Messages In This Thread
RE: Custom controller - request object empty in constructor - by iRedds - 06-24-2022, 04:01 PM



Theme © iAndrew 2016 - Forum software by © MyBB