...
Warning |
---|
|
Using your own controller, it is your responsibility to define cache rules, like for every custom controller ! So don't forget to set cache rules and the appropriate X-Location-Id header in the returned Response object. See built-in ViewController for more details on this. |
Examples
...
Enriching built-in ViewController
This example shows you how to configure and use your own controller to handle a locationa custom controller to enrich the final configured view template. Your controller will here forward the request to the built-in ViewController
with some additional parameters.
Tip |
---|
This is usually the recommended way to use a custom controller. |
Code Block |
---|
|
ezpublish:
system:
ezdemo_frontend_group:
location_view:
full:
myarticle_rulesettest:
# Configuring both controller and template as the controller will forward
# the request to the ViewController which will render the configured template.
controller: AcmeTestBundle:Default:viewFolderarticleViewEnhanced
matchtemplate: AcmeTestBundle:full:article_test.html.twig
Identifier\ContentTypematch:
[folder] Identifier\SectionContentType: [standardarticle] |
Code Block |
---|
language | php |
---|
title | Controller |
---|
|
<?php
namespace Acme\TestBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use eZ\Bundle\EzPublishCoreBundle\Controller;
class DefaultController extends Controller
{
public function viewFolderActionarticleViewEnhancedAction( $locationId, $viewType, $layout = false, )array {
$repository = $this->getRepository();
$params = array() )
{
$location = $repository->getLocationService()->loadLocation( $locationId ); $response = new Response();
$response->headers->set( 'X-Location-Id', $locationId $params += array( 'myCustomVariable' => "Hey, I'm a custom message!" );
// CachingForward forthe 1hrequest andto make the cacheoriginal varyViewController
on user hash // And get $response->setSharedMaxAge( 3600 );
$response->setVary( 'X-User-Hash' );the response. Eventually alter it (here we change the smax-age for cache).
return$response = $this->render>get( 'AcmeTestBundle::custom_controller_folder.html.twig',
array(
'location' => $location,
'content' => $repository->getContentService()->loadContentByContentInfo( $location->getContentInfo() ),
'foo' => 'Hey world!!!',
'ez_content' )->viewLocation( $locationId, $viewType, $layout, $params );
'osTypes' => array( 'osx', 'linux', 'losedows' )$response->setSharedMaxAge( 600 );
)
)return $response;
}
}
|
Code Block |
---|
title | customarticle_controller_foldertest.html.twig |
---|
|
{% extends noLayout ? viewbaseLayout : "eZDemoBundle::pagelayout.html.twig" %}
{% block content %}
<h1>{{ ez_render_field( content, 'title' ) }}</h1>
<h1><h2>{{ foomyCustomVariable }}</h1>h2>
<ul>
{% for os in osTypes %}
<li>{{ os }}</li>
{% endfor %}
</ul>
{% endblock %}
|
Enriching the built-in ViewController
...
{{ ez_render_field( content, 'body' ) }}
{% endblock %} |
Using a custom controller to get full control
This example shows you how to configure and use your own controller to handle a location.
Code Block |
---|
|
ezpublish:
system:
ezdemo_frontend_group:
location_view:
full:
articlemy_testruleset:
# Configuring both controller and template as the controller will forward
# the request to the ViewController which will render the configured template.
controller: AcmeTestBundle:Default:articleViewEnhancedviewFolder
templatematch:
AcmeTestBundle:full:article_test.html.twig match Identifier\ContentType: [folder]
Identifier\ContentTypeSection: [articlestandard] |
Code Block |
---|
language | php |
---|
title | Controller |
---|
|
<?php
namespace Acme\TestBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use eZ\Bundle\EzPublishCoreBundle\Controller;
class DefaultController extends Controller
{
public function articleViewEnhancedActionviewFolderAction( $locationId, $viewType, $layout = false, array)
$params = array() ) {
$repository = $this->getRepository();
{ $location = $repository->getLocationService()->loadLocation( $locationId );
$params += array( 'myCustomVariable' => "Hey, I'm a custom message!" $response = new Response();
$response->headers->set( 'X-Location-Id', $locationId );
// ForwardCaching thefor request1h toand make the originalcache ViewControllervary on user hash
// And get the response. Eventually alter it (here we change the smax-age for cache).$response->setSharedMaxAge( 3600 );
$response->setVary( 'X-User-Hash' );
$responsereturn = $this->get>render(
'ez_content' )->viewLocation( $locationId, $viewType, $layout, $params ); 'AcmeTestBundle::custom_controller_folder.html.twig',
array(
'location' => $location,
'content' => $repository->getContentService()->loadContentByContentInfo( $location->getContentInfo() ),
'foo' => 'Hey world!!!',
$response->setSharedMaxAge( 600 ); 'osTypes' => array( 'osx', 'linux', 'losedows' )
return $response )
);
}
}
|
Code Block |
---|
title | articlecustom_testcontroller_folder.html.twig |
---|
|
{% extends noLayout ? viewbaseLayout : "eZDemoBundle::pagelayout.html.twig" %}
{% block content %}
<h1>{{ ez_render_field( content, 'title' ) }}</h1>
<h2><h1>{{ myCustomVariablefoo }}</h2>h1>
{{ ez_render_field( content, 'body' ) }}
{% <ul>
{% for os in osTypes %}
<li>{{ os }}</li>
{% endfor %}
</ul>
{% endblock %}
|
Overriding the built-in ViewController
...