Welcome Guest, Not a member yet? Register   Sign In
problem with custom controller constructor

(This post was last modified: 06-23-2022, 12:01 AM by groovebird.)


in the constructor i loaded models, settings and checked if access for the user is possible. All things i need throughout the controller. This is a good place until i need access to the request object. Why is this bad practice? What is the better way except overwriting the initController function or loading the request service ?

Bad practice may have been an exaggeration.

Of course you can use __construct() unless you don't useĀ  the request object or something that initController() initializes.
True bad practice is try to use uninitialized things. You don't need to do that.

If you want to do something with the request object, you can use Controller Filters.

I solved this by instantiating the following classes which are already instantiated in BaseController, parent class:
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

Theme © iAndrew 2016 - Forum software by © MyBB