FOF30\Controller\Controller::display PHP Method

display() public method

YOU MUST NOT USE THIS TASK DIRECTLY IN A URL. It is supposed to be used ONLY inside your code. In the URL, use task=browse instead.
public display ( boolean $cachable = false, boolean $urlparams = false, string $tpl = null ) : void
$cachable boolean Is this view cacheable?
$urlparams boolean Add your safe URL parameters (see further down in the code)
$tpl string The name of the template file to parse
return void
    public function display($cachable = false, $urlparams = false, $tpl = null)
    {
        $document = $this->container->platform->getDocument();
        if ($document instanceof \JDocument) {
            $viewType = $document->getType();
        } else {
            $viewType = $this->input->getCmd('format', 'html');
        }
        $view = $this->getView();
        $view->setTask($this->task);
        $view->setDoTask($this->doTask);
        // Get/Create the model
        if ($model = $this->getModel()) {
            // Push the model into the view (as default)
            $view->setDefaultModel($model);
        }
        // Set the layout
        if (!is_null($this->layout)) {
            $view->setLayout($this->layout);
        }
        $conf = $this->container->platform->getConfig();
        if ($this->container->platform->isFrontend() && $cachable && $viewType != 'feed' && $conf->get('caching') >= 1) {
            // Get a JCache object
            $option = $this->input->get('option', 'com_foobar', 'cmd');
            $cache = \JFactory::getCache($option, 'view');
            // Set up a cache ID based on component, view, task and user group assignment
            $user = $this->container->platform->getUser();
            if ($user->guest) {
                $groups = array();
            } else {
                $groups = $user->groups;
            }
            $importantParameters = array();
            // Set up safe URL parameters
            if (!is_array($urlparams)) {
                $urlparams = array('option' => 'CMD', 'view' => 'CMD', 'task' => 'CMD', 'format' => 'CMD', 'layout' => 'CMD', 'id' => 'INT');
            }
            if (is_array($urlparams)) {
                /** @var \JApplicationCms $app */
                $app = \JFactory::getApplication();
                $registeredurlparams = null;
                if (!empty($app->registeredurlparams)) {
                    $registeredurlparams = $app->registeredurlparams;
                } else {
                    $registeredurlparams = new \stdClass();
                }
                foreach ($urlparams as $key => $value) {
                    // Add your safe url parameters with variable type as value {@see JFilterInput::clean()}.
                    $registeredurlparams->{$key} = $value;
                    // Add the URL-important parameters into the array
                    $importantParameters[$key] = $this->input->get($key, null, $value);
                }
                $app->registeredurlparams = $registeredurlparams;
            }
            // Create the cache ID after setting the registered URL params, as they are used to generate the ID
            $cacheId = md5(serialize(array(\JCache::makeId(), $view->getName(), $this->doTask, $groups, $importantParameters)));
            // Get the cached view or cache the current view
            $cache->get($view, 'display', $cacheId);
        } else {
            // Display without caching
            $view->display($tpl);
        }
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Overrides the default display method to add caching support
  *
  * @param   bool        $cachable  Is this a cacheable view?
  * @param   bool|array  $urlparams Registered URL parameters
  * @param   null|string $tpl       Sub-template (not really used...)
  */
 public function display($cachable = false, $urlparams = false, $tpl = null)
 {
     $cachable = true;
     if (!is_array($urlparams)) {
         $urlparams = [];
     }
     $additionalParams = array('option' => 'CMD', 'view' => 'CMD', 'task' => 'CMD', 'format' => 'CMD', 'layout' => 'CMD', 'id' => 'INT');
     $urlparams = array_merge($additionalParams, $urlparams);
     parent::display($cachable, $urlparams, $tpl);
 }