How to extend Controller/RestApi.php or Admin.php of (core) modules

I have a few things in mind, where I would like to extend Admin.php or RestApi.php, like

  • change single function to provide some more options or to replace views in backend
  • add new paths with custom views of collections or forms etc. in backend
  • ModuleList (While thinking about module-link fields, I thought, an api endpoint /api/cockpit/listModules would be a nice-to-have, too)
  • CockpitLite (minimal backend without Javascript)
  • addon with custom fields, that can be extended with other addons (e. g. with special user defined fields)

I didn’t find a way to extend controllers or re-bind classes of modules. I tried a few things and it’s possible to overwrite complete controllers with

$app->on('admin.init', function() {
    include_once(__DIR__.'/Controller/Admin.php');
    include_once(__DIR__.'/Controller/RestApi.php');
});

if the included files have the same namespace and the same class names like the originals, since routes are bind to class names and not to pathes of controller files and addons are loaded later, than core modules.

But overwriting would mean to lose all new updates in the original controllers and I have to copy-paste the whole thing. I did this before in the FormValidation addon, where I only changed one line in Admin.php.

I also tried to bind new classes to routes. Either I didn’t find the right place or rebinding is not possible. I was able to bind classes to pathes, that weren’t registered before. If it’s possible, I could write a new class CustomAdmin extends Admin, that extends \Collections\Controller\Admin with a new function or I could overwrite a single function and use it as a custom controller.

I know, how to add custom api endpoints to /config/api/collections/endpoint.php, too, but if I want to extend api endpoints with an addon to endpoints like /api/collections/custom or /api/cockpit/custom, I have no idea, how to do it.

I also know, that I can build an addon with its own Admin.php and RestApi.php and call it with /api/addonname/custom. But if the addon is just a simple extension to a core module, I would like to call it with the module endpoints.

It seems, it was possible in 2014

Thus - is it possible?

Small update:

Some tests with invoking classes failed because of an automatic redirect of collection entries view.

Reusing existing classes and extending them is possible. :slight_smile:

I’ll give it another try in the next days.