2

I have made a custom component, also with help from component-creator.com. But I am struggling with display methods. For example I want to check something in the display method, but when I try to add that the display method isn't even called. it seems that it only calls from the base controller.

I don't understand why it works like this.

rootcomponent/controller.php

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

jimport('joomla.application.component.controller');

use \Joomla\CMS\Factory;

/**
 * Class MatchesController
 *
 * @since  1.6
 */
class MatchesController extends \Joomla\CMS\MVC\Controller\BaseController
{
    /**
     * Method to display a view.
     *
     * @param   boolean $cachable  If true, the view output will be cached
     * @param   mixed   $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
     * @throws Exception
     */
    public function display($cachable = false, $urlparams = false)
    {
        
        $app  = Factory::getApplication();
        $view = $app->input->getCmd('view', 'matches');
        $app->input->set('view', $view);

        $user = Factory::getUser();

        if ($user->get('guest') == 1) {
            $this->setRedirect(JRoute::_('index.php?option=com_users&view=login&return=' . base64_encode(JUri::current()), "You must be logged in to view this content"));
            return;
        }

        parent::display($cachable, $urlparams);

        return $this;
    }
}

rootcomponent/controller/matches.php

use Joomla\CMS\MVC\Controller\BaseController;

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

/**
 * Matches list controller class.
 *
 * @since  1.6
 */
class MatchesControllerMatches extends BaseController
{
    public function display($cachable = false, $urlparams = false)
    {
        echo "See if display function is called here";
        die();

    }

    /**
     * Proxy for getModel.
     *
     * @param   string  $name    The model name. Optional.
     * @param   string  $prefix  The class prefix. Optional
     * @param   array   $config  Configuration array for model. Optional
     *
     * @return object   The model
     *
     * @since   1.6
     */
    public function &getModel($name = 'Matches', $prefix = 'MatchesModel', $config = array())
    {
        $model = parent::getModel($name, $prefix, array('ignore_request' => true));

        return $model;
    }

}

rootcomponent/matches.php

defined('_JEXEC') or die;

use \Joomla\CMS\Factory;
use \Joomla\CMS\MVC\Controller\BaseController;

// // Include dependancies
// // jimport('joomla.application.component.controller');

JLoader::registerPrefix('Matches', JPATH_COMPONENT);
JLoader::register('MatchesController', JPATH_COMPONENT . '/controller.php');


// Execute the task.
$controller = BaseController::getInstance('Matches');
$controller->execute(Factory::getApplication()->input->get('task'));
$controller->redirect();

I don't know if you need more code?

10
  • Could you share some code?
    – Lodder
    Dec 20, 2021 at 15:47
  • @Lodder i have added some extra code
    – Huub S
    Dec 20, 2021 at 16:16
  • @HuubS - if you call the display method with …&task=matches.display it should call the display() method of that controller. Does it not?
    – Zollie
    Dec 20, 2021 at 19:49
  • According to my tests, if you call the method of that subcontroller explicitely in URL, the controller is going to be called and it will respond with the method, but this is not your fault here that Joomla does not call the display() of that subcontroller as default, it is Joomla’s illogic and that is why I wrote what I wrote earlier on this. You rightfully expect that display() method of that subcontroller is going to be called even if you just call the controller only. Instead of that, Joomla automatically calls the View display() function (if it is not there, then the default tmpl file is
    – Zollie
    Dec 21, 2021 at 8:15
  • called (in matches View folder).
    – Zollie
    Dec 21, 2021 at 8:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.