Possibility to merge/extend controller from registry

Is it possible to merge or extend a controller set in the registry via a plugin?

I know you can “override” or “discard” the controller by defining a new one with the same name in /site/controllers, but then the logic defined in the plugin controller is discarded. I would like to have some basic functionality in a controller in a plugin that can be “extended” in a “standard” controller without copying code over from the plugin.

:crossed_fingers:

At least not that I know of, unless you create a sort of default controller script and then include that file in your site controller.

Darn, I thought so. I would rather not fiddle with default controllers etc as that kinda defeats my purpose of moving code in a plugin (which could be used as an agnostic component to set a functional baseline).

Then maybe rather than using a controller, you can outsource that code into a function or class?

Hmmm, yes. But then I wouldn’t have a “default” controller which is automagically there when there is no local controller defined.

Unless I create a controller in the plugin which simply runs the same function? I’ll give it a try. Thx

For what it’s worth, let me add this link to an old discussion: Inherit Controller - Controllers that inherit from the site controller

No solution for this now? I’m stucking with that too. I found this article. It’s pretty useful. But still, if you use a site controller in your Plugin, it will die if there’s a page controller around.

I made a suggestion for that.

@texnixe Do you have an code example for your…

It all depends on what you are trying to achieve. A lot of functionality can for example go into a page model. So you could for example have a base page model and your dedicated page models would extend this base page model, thus sharing all these methods.

Or you create a data provider class that defines some methods with your logic which you can then reuse in all your controllers or directly in the templates.

Also, you can make extensive use of collections.

It’s nothing that is then automatically loaded everywhere, but on the other you don’t have to overwrite stuff from your shared controller.

Hm. I trapped into this again. Got no satisfy solution for that.

What i achieve to do is, to have variable available for my methods. Something like this:
$foo->myMethod() or even better: foo()->myMethod().

What i don’t want is, to work with any site/collection/blocks/fields… methods (like $site->foo()). I need my own scope, without drowning any of these classes.

The site controller seems to be the only solution for that. But then i override the other site controllers. It’s not fair if i create a plugin for the community and take them the opportunity to use their own site controller.

well, if foo()->myMethod() is “even better” and you don’t care about $foo; you can just create your own function anywhere, either in a plugin or simply in config.php or index.php.

site/plugins/foo/index.php:

<?php

class Foo {
    public function myMethod() {
        return "bar!";
    }
}

function foo() {
    return new Foo();
}