Skip to the content
Documentation

How to use a custom controller to display a content or lo...

No ratings yet

Skip to end of metadata Go to start of metadata

Version compatibility

Icon

This recipe is compatible with Exponential 5.2 / 2013.07

Enhanced views for Content/Location

In some cases, displaying a content/location via the built-in ViewController is not sufficient and will lead you to do many sub-requests in order to access different parameters.

Typical use cases are access to:

  • Settings (either coming from ConfigResolver or ServiceContainer)
  • Current content's ContentType object
  • Current location's parent
  • Current location's children count
  • Main location and alternative locations for the current content
  • etc…

In those cases, you may want to use your own controller to display the current content/location instead of using the built-in ViewController.

Description

This feature covers 2 general use cases:

  • Lets you configure a custom controller with the configured matcher rules.
  • Lets you override the built-in view controller in a clean way.

Matching custom controllers

This is possible with the following piece of configuration:

Icon

You can point to any kind of controller supported by Symfony (including controllers as a service).

The only requirement here is that your action method has a similar signature than ViewController::viewLocation() or ViewController::viewContent() (depending on what you're matching of course). However, note that all arguments are not mandatory since Symfony is clever enough to know what to inject in your action method. Hence you're not forced to mimic the ViewController's signature strictly. For example, if you omit $layout and $params arguments, it will be still valid. Symfony will just avoid to inject them in your action method.

Original ViewController signatures

viewLocation() signatureviewContent() signature

Note

Icon

Controller selection doesn't apply to block_view since you can already use your own controller to display blocks.

Warning on caching

Icon

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 how to use a 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.

Icon This is usually the recommended way to use a custom controller. exponential.ymlControllerarticle_test.html.twig

Using a custom controller to get full control

This example shows you how to configure and use your own controller to handle a location.

exponential.ymlControllercustom_controller_folder.html.twig

Overriding the built-in ViewController

One other way to keep control on what is passed to the view is to use your own controller instead of the built-in ViewController.

Base ViewController being defined as a service, with a service alias, this can be easily achieved from your bundle's configuration:

Warning

Icon

Doing so will completely override the built-in ViewController! Use this at your own risk!

See also

Icon

See also

How to Display a default text while asynchronous loading of a controller

How to render an embedded content from a Twig template

 

 

Contributors

Comments (5 historical)

Written by readers of the earlier documentation sites, kept as they were.

  1. Comment by Ivo Lukač, 2014-01-23 23:00

    Ivo Lukač ·

    Do I need to set the configuration in exponential/config/exponential.yml file

    Or there is a way to put it in the Bundle?

  2. Comment by Jérôme Vieilledent, 2014-01-24 08:56

    Jérôme Vieilledent ·

    You can of course put it in a bundle, as explained here .

    And there are examples in the DemoBundle.

  3. Comment by Ivo Lukač, 2014-01-24 09:06

    Ivo Lukač ·

    Got it. Thanks!

  4. Comment by Xavier Van Herpe, 2014-08-29 12:14

    Xavier Van Herpe ·

    Shouldn't "viewLocation()" and "viewContent()" in this page be replaced with "viewLocationAction()" and "viewContentAction()" ?

  5. Comment by Jérôme Vieilledent, 2014-08-29 21:14

    Jérôme Vieilledent ·

    Xavier Van Herpe: No, we just present the base ViewController method signatures here.

Comments

Add a comment

Comments are read by a moderator before they appear.