Enhanced views for Content/Location
In some cases, displaying a Content item/Location via the built-in
ViewController is not sufficient and will force you to make many sub-requests in order to access different parameters.
Typical use cases are access to:
- Settings (either coming from
- Current Content item's
- Current Location's parent
- Current Location's children count
- Main Location and alternative Locations for the current Content item
In those cases, you may want to use your own controller to display the current Content item/Location instead of using the built-in
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:
The only requirement here is that your action method has a similar signature than
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 into your action method . That is why you aren't forced to mimic the
ViewController's signature strictly. For example, if you omit
$params arguments, it will still be valid. Symfony will just avoid injecting them into your action method.
Original ViewController signatures
Controller selection doesn't apply to
block_view since you can already use your own controller to display blocks.
Warning on caching
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
See built-in ViewController for more details on this.
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.
Always ensure that you add new parameters to existing
$params associative array, using
+ union operator or
Not doing so (e.g. only passing your custom parameters array) can result in unexpected issues with content preview. Previewed content and other parameters are indeed passed in
Using a custom controller to get full control
This example shows you how to configure and use your own controller to handle a location.
Overriding the built-in ViewController
One other way to keep control of 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:
Doing so will completely override the built-in ViewController! Use this at your own risk!