eZ Community » Forums » eZ Publish 5 Platform » Solution: Caching for restricted...
expandshrink

Solution: Caching for restricted areas and legacy templates

Solution: Caching for restricted areas and legacy templates

Tuesday 27 May 2014 3:54:20 pm - 2 replies

Imaging following situation:

You have a restricted area with unique content per user and not per user group (-> no Context aware HTTP cache https://doc.ez.no/display/EZP/Context+aware+HTTP+cache )

and some legacy templates. 

A setting the ttl to 0 in legacy templates with {set-block scope=root variable=cache_ttl}0{/set-block} has no effect, you will need to use an alternative. There are two possibilities:

1. Make the user hash whichs controls caching unique, as described in https://doc.ez.no/display/EZP/How+to+customize+UserHash+generation.
This results in mainly useless caches.
2. Mark the response header as private, which prevents caching:

Write an own controller.

https://doc.ez.no/display/EZP/How+to+use+a+custom+controller+to+display+a+content+or+location#Howtouseacustomcontrollertodisplayacontentorlocation-Enrichingbuilt-inViewController

BUT: The example in documentation is only for eZ Symfony results, legacy results will not work!

If you have legacy templates in use, make it like this:

Go into your src/CjwNetwork/SiteCustomerBundle/Resources/config/override.yml and add an matching for your internal area:

system:
    customer_user_de:
        location_view:
            full:
                section_internal:
                    # Configuring both controller and template as the controller will forward
                    # the request to the ViewController which will render the configured template.
                    controller:  "CjwSiteCustomerBundle:Cjw:sectionInternal"
        # you can take this for a eZ Symfony result, not for legacy template results
                    #  template: "CjwSiteCustomerBundle:Cjw:full:legacy.html.twig"
                    match:
                        Identifier\Section: internal

in your src/CjwNetwork/SiteCustomerBundle/Controller/CjwController.php

<?php

namespace CjwNetwork\SiteCustomerBundle\Controller;
use eZ\Bundle\EzPublishCoreBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
use eZFunctionHandler;



class CjwController extends Controller
{
    /**
     * Sets a new header for private sections
     *
     * @param $locationId
     * @param $viewType
     * @param bool $layout
     * @param array $params
     *
     * @var $kernel  \eZ\Publish\Core\MVC\Legacy\Kernel
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */

    public function sectionInternalAction($locationId, $viewType, $layout = false, array $params = array())
    {


        $response = new Response();
// get the legacy kernel
        $kernel = $this->getLegacyKernel();
// get rid of the pagelayout.tpl
        $kernel->setUsePagelayout(false);
// get the rendered content
        $legacy_content = $kernel->run()->getContent();
// put the rendered content into the twig template and add the locationId for getting breadcrump etc.
        $response = $this->render('CjwSiteCustomerBundle:full:legacy.html.twig', array('legacy_content' => $legacy_content, 'locationId' => $locationId));
// set the header to private,
        $response->setPrivate();
        $response->headers->set('X-Location-Id', $locationId);
        $response->setVary('X-User-Hash');

        return $response;
    }
}


the template full/legacy.html.twig

{% extends 'CjwSiteCustomerBundle::pagelayout.html.twig' %}
 
{% block content %}
 
   {{ legacy_content|raw }}
 
{% endblock %}
 
<strong>

Greetings, ekke

PS: Thanks to Donat http://share.ez.no/community/profile/10451

Modified on Tuesday 27 May 2014 4:30:00 pm by Ekkehard Dörre

Wednesday 28 May 2014 4:32:40 pm

Hi Ekke

The magic of serendipity showed again...

Strolling around I came across another solution. Jérôme faced a similar problem in his Metalfrance project, and of course, he has an elegant answer:

https://github.com/lolautruche/metalfrance/blob/master/src/MetalFrance/LegacyBundle/Controller/SidebarController.php

I would recommend all eZ 5 newbies (meaning all of us) to study this project. Jérôme has updated it very recently and it provides a wealth of best practice and tips and tricks not yet in the documentation.

Cheers,
Donat

Friday 30 May 2014 12:30:27 pm

Hi Donat !

I'm glad that Metalfrance code helped you! It was the goal when I decided to open source it, so this makes me happy happy.gif Emoticon.

Just one thing in addition to your finding : I had to change the order of template engines in ezpublish/config/config.yml, and place eztpl before twig. Otherwise Twig template name parser complains about forbidden characters.

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from