Customising components
Components can be added in Photon by creating a controller class and defining a service tagged as photon.component
.
The controller class for a component is a standard Symfony controller that takes a Request
and returns a Response
. The controller must be an invokable class to be compatible with the component system.
The service must be given a name. This name will be used when referencing the component within a Twig template.
Once the component service is defined it can be rendered within any Photon template using the component()
Twig function.
When creating the service, the parent service photon.controller.component
can be used to simplify the definition. This will set all of the dependencies that are required by the Component
base class.
Defining the controller:
<?php
namespace Spacecraft\DemoProject\Component;
use Photon\Core\Controller\Component;
use Symfony\Component\HttpFoundation\Response;
class AnnouncementController extends Component
{
/**
* @return Response
*/
public function __invoke()
{
return $this->render('components/announcement.html.twig');
}
}
Defining the service:
spacecraft_demo_project.page.demo:
class: Spacecraft\DemoProject\Component\AnnouncementController
parent: photon.controller.component
tags:
- { name: photon.component, component_name: announcement }
Invoking the component:
{{ component('announcement') }}