Skip to main content

Components

Component controllers, like page controllers, are responsible for fetching the content from a respository and rendering a template.

A simple invokable controller class is defined per component.

Page controllers have no knowledge of the action of the component controller, components are rendered within a page template as sub-requests.

As with Pages, Components can be extended and overridden by other themes.

Additional information on sub-requests is provided in the Symfony documentation - Sub Requests.

Components are registered within the theme services.yml configuration file.

photon_cms_project.component.category_navigation:
class: CategoryNavigationController
parent: photon.controller.component
arguments:
- '@ohoton.repository.category'
- '@router'
- '@sdk.configuration.constant_repository'
tags:
- { name: photon.component, component_name: category_navigation }

Component services must be tagged with photon.component.

Invoking components

Components are not invoked by a route, but instead by using the component() Twig function

{{ component('announcement') }}
{{ component('announcement', {name: 'Tom'}) }}

Parameters can be passed to components at render time, which are passed to the __invoke() method of the Component.

<?php

namespace Spacecraft\DemoProject\Component;

use Photon\Core\Controller\Component;
use Symfony\Component\HttpFoundation\Response;

class CategoryNavigationController extends Component
{
/**
* @param string $name
*
* @return Response
*/
public function __invoke($name)
{
return $this->render(
'announcement.html.twig',
[
'name' => $name,
]
);
}
}