Post by richcon » Mon Jul 19, 2010 11:06 am

Better Plugin System:

OpenCart could use a way to add content to existing modules, in an MVC-compliant way, without having to rewrite core files to do it. For example, people could create modules adding similar product views or videos to Product pages by registering their modules as children of the core module 'product/product'.

This would involve a relatively simple (and elegant) addition to the Controller class and a new relations table to the database. Since all the work is done in the Controller class, all controllers would automatically inherit this feature.

New database table: ControllerChildren, with fields "parent", "child", "child_action" (the "action" method to call, i.e. "index")

Protected Controller methods:

Code: Select all

    Controller->addParent(path, action='')
    Controller->removeParent(path)
    Controller->removeParents() 


Private Controller method:

Code: Select all

    Controller->loadChildren() // Run from Controller->render 
(I would suggest two more Protected methods, addChild and addChildren, which would add to the 'children' property in a more OOP-compliant way. Data hiding is good practice.)

- Module Controller objects can register themselves as children of existing modules with method "addParent(parent [, action method])" from within their 'install' method. Their 'uninstall' method can deregister itself either one by one with $this->removeParent(path) or all at once with $this->removeParents().
- Then Controller->render would be modified with one extra line: $this->loadChildren().
- Controller->loadChildren, then, loads all registered children from the database and adds them to $this->children; if child_action is set that is added as the 'action' component of the path; otherwise path is left blank (and 'index' is called as the default action).


Example:

In a hypothetical "similarProducts" module which displays products similar to the currently viewed one, the following would be placed in its Controller:

Code: Select all

    function install() {
        $this->addParent(/* Parent path */ 'product/product', /* Action method */ 'product');
    }

    function uninstall() {
        $this->removeParents();
    }

    function product($product_id) {
        // The actual action method. This will be called from the Product controller
        // Its name was given in the above 'addParent" method call.
        
        // Load model and template file then execute, as with any other action
    } 
And in the user's custom Product template, include is the same as for any other child module. Wrapped in "isset" to avoid PHP warnings when the similarProducts module isn't loaded.

Code: Select all

    if (isset($similarProducts)) {
        echo $similarProducts;
    } 

New member

Posts

Joined
Thu Apr 15, 2010 7:58 am
Location - Los Angeles
Who is online

Users browsing this forum: No registered users and 2 guests