Jump to content

Module front Controller cache


Recommended Posts

Hello , 
i have a custom module with front controller , its tpl file includes a hook as below :
 

{block name='faq'}
    {hook h='DisplayFaqWrapper' mod='PsCustom'}
{/block}

Below , is my hook , I used to manage cache   but it doesnt work :

 

<?php

namespace PrestaShop\Module\PsCustom\Hook;

use Cache;
use Context;
use PsCustom;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use PrestaShop\Module\PsCustom\Entity\PsCustomCategory;
use PrestaShop\Module\PsCustom\Service\PsCustomCategoryService;
use Tools;

/**
 * Trait DisplayFaqHook
 * @package PrestaShop\Module\PsCustom\Hook
 */
trait DisplayFaqHook
{
    /** @var string */
    public $template = 'module:' . PsCustom::MODULE_NAME . '/views/templates/hook/faq.tpl';

    /** @var string */
    public $faqCacheId = PsCustom::MODULE_NAME . '|hookDisplayFaqWrapper';

    /**
     * Render faq template from cache
     * @throws Exception
     */
    public function hookDisplayFaqWrapper(): string
    {
        try {
            /** @var PsCustomCategoryService $faqService */
            $faqService = $this->get('prestashop.module.ps_custom.service.ps_custom_category');

            //if (Tools::isSubmit('submitFaq')) {
            $searchFaq = htmlspecialchars_decode(trim(Tools::getValue('searchFaq')));
            $data['searchFaq'] = $searchFaq;
            //}
//            var_dump($this->getCacheId('PsCustom'));
//            var_dump($this->isCached($this->template));
//            var_dump($this->isCached($this->template, $this->getCacheId($this->faqCacheId)));

            if (! $this->isCached($this->template, $this->getCacheId($this->faqCacheId))) {
                // die('block 1');
                $data['PsCustomCategories'] = $faqService->getFaqByCategories($searchFaq);
                $this->context->smarty->assign('data', $data);
            } else {
                die('block 2');
            }

            if (Tools::isSubmit('submitFaq') || Tools::isSubmit('clear')) {
                $this->context->smarty->assign('data', $data);
                return $this->fetch($this->template);
            }

            //die('not in cache block');

            return $this->fetch($this->template, $this->getCacheId($this->faqCacheId));
        } catch (Exception $exception) {
            error_log('CPM LOG: ' . __METHOD__ . ' ' . $exception->getMessage() . ' ' . $exception->getTraceAsString());
            return false;
        }
    }

}

This instruction always return false :
 

$this->isCached($this->template, $this->getCacheId($this->faqCacheId))

 

Any idea to fix the cache ?

Thanks in advance

Link to comment
Share on other sites

in this filter context, I don't want to cache the search result, otherwise it will be shared with all users, so I've put in 2 templates

     if (Tools::isSubmit('submitFaq') || Tools::isSubmit('clear')) {
                $this->context->smarty->assign('data', $data);
                return $this->fetch($this->template);
            }

 

Link to comment
Share on other sites

il y a 16 minutes, JulienJacob a dit :

 is there a prestashop module with front controller that uses cache? 

It seems that your module is not a ModuleFrontController, but a module with a custom hook.

You should add $params to your custom hook

 public function hookDisplayFaqWrapper($param): string
    

Do you make a $this->registerHook('displayFaqWrapper'); in your install() function?


Try

$cacheId = $this->getCacheId($this->faqCacheId);
if (! $this->isCached('faq.tpl', $cacheId)) {

and

return $this->display(__FILE__, 'faq.tpl', $cacheId);

 

Link to comment
Share on other sites

8 minutes ago, Prestashop Addict said:

It seems that your module is not a ModuleFrontController, but a module with a custom hook.

You should add $params to your custom hook

 public function hookDisplayFaqWrapper($param): string
    

Do you make a $this->registerHook('displayFaqWrapper'); in your install() function?


Try

$cacheId = $this->getCacheId($this->faqCacheId);
if (! $this->isCached('faq.tpl', $cacheId)) {

and

return $this->display(__FILE__, 'faq.tpl', $cacheId);

 


return $this->display(__FILE__, 'faq.tpl', $cacheId);  makes an error : no template found

Yes my custom module have a front controller , wich includes a custom hook 

 

<?php

/**
 * Class PsCustomDisplayFaqModuleFrontController
 */
class PsCustomDisplayFaqModuleFrontController extends ModuleFrontController
{
    /** @var string */
    public $templateFaq = 'module:' . PsCustom::MODULE_NAME . '/views/templates/front/wrapper.tpl';


    public function __construct()
    {
        parent::__construct();
    }


    /**
     * @see FrontController::initContent()
     */
    public function initContent()
    {
        parent::initContent();
        $this->setTemplate($this->templateFaq);
    }


    /**
     * Method called by default to add specify js and css for the view called by the initContent function
     */
    public function setMedia(): void
    {
        parent::setMedia();
        $this->registerStylesheet(
            'module-PsCustom-display-faq',
            'modules/' . $this->module->name . '/views/css/display-faq.css',
            [
                'media'    => 'all',
                'priority' => 200,
            ]
        );
        $this->registerJavascript(
            'module-PsCustom-display-faq',
            'modules/' . $this->module->name . '/views/js/display-faq.js',
            [
                'media'    => 'all',
                'priority' => 200,
            ]
        );
    }
}

here the faq.tpl : 

{block name='faq'}
    {hook h='DisplayFaqWrapper' mod='cpmfaq'}
{/block}


and the hook code as below 

 

<?php

namespace PrestaShop\Module\PsCustom\Hook;

use Cache;
use Context;
use PsCustom;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use PrestaShop\Module\PsCustom\Entity\PsCustomCategory;
use PrestaShop\Module\PsCustom\Service\PsCustomCategoryService;
use Tools;

/**
 * Trait DisplayFaqHook
 * @package PrestaShop\Module\PsCustom\Hook
 */
trait DisplayFaqHook
{
    /** @var string */
    public $template = 'module:' . PsCustom::MODULE_NAME . '/views/templates/hook/faq.tpl';

    /** @var string */
    public $faqCacheId = PsCustom::MODULE_NAME . '|hookDisplayFaqWrapper';

    /**
     * Render faq template from cache
     * @throws Exception
     */
    public function hookDisplayFaqWrapper(): string
    {
        try {
            /** @var PsCustomCategoryService $faqService */
            $faqService = $this->get('prestashop.module.ps_custom.service.ps_custom_category');

            //if (Tools::isSubmit('submitFaq')) {
            $searchFaq = htmlspecialchars_decode(trim(Tools::getValue('searchFaq')));
            $data['searchFaq'] = $searchFaq;
            //}
//            var_dump($this->getCacheId('PsCustom'));
//            var_dump($this->isCached($this->template));
//            var_dump($this->isCached($this->template, $this->getCacheId($this->faqCacheId)));

            if (! $this->isCached($this->template, $this->getCacheId($this->faqCacheId))) {
                // die('block 1');
                $data['PsCustomCategories'] = $faqService->getFaqByCategories($searchFaq);
                $this->context->smarty->assign('data', $data);
            } else {
                die('block 2');
            }

            if (Tools::isSubmit('submitFaq') || Tools::isSubmit('clear')) {
                $this->context->smarty->assign('data', $data);
                return $this->fetch($this->template);
            }

            //die('not in cache block');

            return $this->fetch($this->template, $this->getCacheId($this->faqCacheId));
        } catch (Exception $exception) {
            error_log('CPM LOG: ' . __METHOD__ . ' ' . $exception->getMessage() . ' ' . $exception->getTraceAsString());
            return false;
        }
    }

}

 

Link to comment
Share on other sites

  • 1 month later...
Le 17/05/2024 à 11:06 AM, JulienJacob a dit :

 is there a prestashop module with front controller that uses cache? 

Tested on 1.7.8.12 In your module implement theses functions

  public function isTemplateCached($template)
  {
	return $this->isCached($template, $this->getCacheId());
  }

  public function myrenderTemplate($template, array $vars)
  {
	if (!$this->isCached($template, $this->getCacheId())) {
		$this->smarty->assign($vars);
	}
    
	return $this->fetch($template, $this->getCacheId());
  }

And in your moduleFrontController

$template = 'module:modulename/views/template/hook/mytemplate.tpl';
$tplVars = [];
if (!$this->module->isTemplateCached($template)) {
	// Fill your tplVars[]
	...
}
$html = $this->module->myrenderTemplate($template, $tplVars);

And if you want to clear the templact cache yourself in the module

parent::_clearCache('module:modulename/views/template/hook/mytemplate.tpl');

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...