Welcome Guest, Not a member yet? Register   Sign In
How to extend the Controller core class

(07-22-2017, 07:46 PM)kilishan Wrote: Showing code you tried and telling the actual error you received is the best way to get a good answer. That said - extending the Controller class is pretty simple - you're just extending a normal PHP class, no special MY_ prefixes needed or anything like that.

1. Make a new BaseController class/file. Make sure it is namespaced so the autoloader can find it.

PHP Code:
<?php namespace App\Controllers;

BaseController extends \CodeIgniter\Controller 
    public function __construct(...$params)

2. Have all of your new controllers extend BaseController instead of CodeIgniter\Controller.

Note: you don't need to have the constructor shown here unless you need additional tasks in the constructor. I just showed it here to remind people about the parameter packing/un-packing available in PHP 7.

What is the proper way to extend the CI4 BaseController where the child class(es) will require a constructor? Very newbie question, but I'm under the impression that if there is an immediate child constructor, one must include...


...in the child's constructor. The "...$params" is the part I'm unsure about. The BaseController constructor (initController) has three params that are required. It's not obvious to me what to include as the params.

Messages In This Thread
How to extend the Controller core class - by Edel - 07-22-2017, 03:35 PM
RE: How to extend the Controller core class - by never2ice - 04-03-2020, 08:40 AM

Theme © iAndrew 2016 - Forum software by © MyBB