CodeIgniter Forums
BaseController & __construct - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: BaseController & __construct (/showthread.php?tid=76438)



BaseController & __construct - shaunM_za - 05-12-2020

Hi

I have a controller that extends BaseController.

I also need to execute some code in the controllers __construct method.

The __construct gets called before the initController function in the BaseController.

Is there a "2ndary" controller init that I can use that will get called after initController ?

PS. I don't want to start using Filters for this.

PHP Code:
class BaseController extends Controller
{
    public function initController()
    {
        
echo 'basecontroller init<br/>';
        
// ..........


class Search extends BaseController
{
    public function __construct()
    {
        echo 'search __construct<br/>';
        
//......... 

OUTPUT:
search __construct
basecontroller init


RE: BaseController & __construct - jreklund - 05-12-2020

You can create your own initController() in your Search class.

PHP Code:
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
{
    
parent::initController($request$response$logger);

    
// Your code here