Controller inheritance

Hi folks,

I’m currently refactoring my project and really need some fresh ideas on how to implement certain stuff.

I have frontend module containing account and profile management controllers. Things are designed quite well

till the moment I created frontend’s submodule (call it blog) having it’s own account & profile properties.

Currently frontend/blog/ProfileController duplicates certain code of frontend/ProfileController, uses extended

model and extended view (all copy some code from the frontend/ProfileController). I need some elegant solution on

how to inherit one controller fron another counting models and views. I’ve got some ideas but none of them are simple and clean enough. So, am kindly asking for your help.

How about creating a BaseFrontendController which contains all shared functions? Then you could extend ProfileController and others from BaseFrontendController.

Sounds reasonable, but I’m not sure this will ever solve view/model inheritance. Will still need to copy my code.

Controller inheritance isn’t that big deal. To avoid this move, we can simply create a ‘modelClass’ property and

change it to an inherited model when nessesary. The most hard thing are views. As far as I remember, in 1.1 we have CForm. Does it implement a builder pattern so I could inherit form views?

The solution making actions and sharing them will not work?

No. While actions inheritance is a good concept, controller (and actions) inheritance is

not an issue here since I use massive assignment and this, in turn, respects inherited model

properties. What I’m trying to achieve here is a generic controller/view couple. This way,

I would only need to have:

  1. class BlogAccountModel extends AccountModel

  2. frontend/ProfileController::$modelClass = ‘BlogAccountModel’;

Again, main problem is making our view as generic as possible and/or

provide an ability to inherit views.

P.S. Looks like I need a kind of cutom scaffolding in YII views…

So an idea is to extend something as zii extensions or something yours at views?

To be short, an idea is to extend (inherit) models/controllers/views reusing the code.

This implies:

  1. Extending controller/actions - native (abstract BaseController).

  2. Extending models - native (class BlogProfileModel extends ProfileModel).

  3. View inheritance - in 1.0 we can’t insert ‘Signature’ field after ‘Full Name’ field.

    I’m currently investigating CForm to figure out if it’s allowing this.

Solved! Solution:

interface IRuntimeConfigProvider


    public function getRuntimeConfig();


Every module/submodule that:

  1. Provides additional profile fields;

  2. Provides backend menu items/sections;

  3. Anything (e.g. tabs for admin dashboard);

implements this interface returning required info as an array.

TWebApplication manages runtime configuration by traversing all

modules recursively, getting runtime config (if any), merging

configuration arrays, caching and returning the result.

This solution matches following requirements:

  1. Every module may provide unbound information for core-ends (espcially backend - admin panel);

  2. Runtime configuration format is defined/controlled by the recipient;

  3. Configuration information may be overridden by child-modules (pseudo-inheritance);

  4. Doesn’t affect application configuration, auto-updates upon plugging/unplugging modules;

Kind regards for everyone who supported me here! PM for code if interested.