How to extend core controllers

Posted on November 12, 2020


The core controllers from Gila CMS are registered in the core load file, and they give the functionality that you need in order to create and update content, or install new packages and themes.

When you want to add new actions or remove actions from a controller, there some easy ways where you dont have to worry of upgrading Gila CMS in new versions.

For example, you want to add/replace a new action for admin controller:

The easiest way is to add a route:

Router::add('admin/action/(.*)', function($x){
  echo 'Route: admin/action/'.$x; 
}, 'GET', 'admin');

The above example works fine, because it gives access only on users with admin privilege. But this method only runs an anonymous function, and dont run the common user checks that admin controller has for all its actions. In order to run first the controller's constructor, you can use this method:

Router::action('admin', 'action', function($x){
  global $c;
  if (Session::hasPrivilege('admin')===false) {

This method replaces the call of the actual method from the admin controller. The downside is that it cannot acces the controller attributes with the self keyword, but you need to use the global $c, that is the instance of the controller.

The advantage is that you can change or add a new action, without causing problems in other packages, that also want to extend an action. So its use, is for a package that does a small change in a controller.

Class extention

Finally, if your code is used as the main application code, you can extend the controller class:

class MyAdminController extends corecontrollersAdminController {

  // add/replace action
  public function oneAction() {...}

  // remove an action
  public function twoAction() {
    // run the controller's index, like this action does not exist

And also, register the controller in your load.php with the new class:

Router::controller('admin', 'my_package/controllers/MyAdminController', 'MyAdminController');

Extending the class, you can make more changes easier, with clean code and your class will extend the new updated versions of the AdminController class.

Subscribe to our newsletter