can't seem to override display() method in a view controller

62 views
Skip to first unread message

leo.van...@gmail.com

unread,
Sep 25, 2014, 10:09:07 AM9/25/14
to joomla-de...@googlegroups.com
Hi,

I'm writing a component and for this component a view needs access to two different models. I found http://docs.joomla.org/Using_multiple_models_in_an_MVC_component which is helpfull, however I want to override the default display() method in my controller so I can add the models, this is were i run into trouble. This is the controller.php in the root of component:

class MycomponentController extends JControllerLegacy {

    /**
     * Method to display a view.
     *
     * @param boolean $cachable If true, the view output will be cached
     * @param array $urlparams An array of safe url parameters and their variable types, for valid values see {@link JFilterInput::clean()}.
     *
     * @return JController This object to support chaining.
     * @since 1.5
     */
    public function display($cachable = false, $urlparams = false) {
        require_once JPATH_COMPONENT . '/helpers/mycomponent.php';
        
        $view = JFactory::getApplication()->input->getCmd('view', 'organisations');
        $task = JFactory::getApplication()->input->getCmd('task', 'display');
        JFactory::getApplication()->input->set('view', $view);
        
        parent::display($cachable, $urlparams);

        return $this;
    }

}


I wanted to override this in my view by adding a display method in my organisation view controller in the controllers directory:

// No direct access
defined('_JEXEC') or die;

require_once JPATH_COMPONENT . '/controller.php';

/**
 * Organisation controller class.
 */
class MycomponentControllerOrganisation extends MycomponentController {

    /**
     * Method to display a view.
     *
     * @param boolean $cachable If true, the view output will be cached
     * @param array $urlparams An array of safe url parameters and their variable types, for valid values see {@link JFilterInput::clean()}.
     *
     * @return JController This object to support chaining.
     * @since 1.5
     */
    public function display($cachable = false, $urlparams = false) {
        echo 'overriden';
        
        parent::display();
    }


But this is never executed and I have no idea why... Can anybody help me out here?

Thanks in advance.

Leo

Bakual

unread,
Sep 25, 2014, 11:58:06 AM9/25/14
to joomla-de...@googlegroups.com
What you're trying to do is calling a subcontroller. To call that one, you would have to use the URL parameter "&task=organisation.display" which will load the controller located at "controllers/organisation.php" and call the "display" method in it.

In your case, the task is only "display" (the default Joomla task). So it will never look at the subcontroller.

However you don't necessary need a subcontroller. I do load additional models in my regular controller as well. See https://github.com/Bakual/SermonSpeaker/blob/master/com_sermonspeaker/site/controller.php#L71 for an example how it could be done.
Reply all
Reply to author
Forward
0 new messages