Configure your resource
Last updated
Last updated
Read the previous chapter to . In order for your resource to truly become a Sylius Resource
,
you will need to configure a couple of things.
First, to declare your resource as a Sylius Resource, implement the Sylius\Component\Resource\Model\ResourceInterface
,
which requires defining a getId() method to uniquely identify the resource.
Next, add the #[AsResource]
PHP attribute to your Doctrine entity to register it as a Sylius resource.
Run the following command to verify that your resource is correctly configured.
By default, the alias for your Sylius resource will be app.book
, which combines the application name and the resource name
with this format : {application}.{resource}
.
You can override your resource's name via the name
parameter of the AsResource
PHP attribute.
In this example, the order
variable is replaced with cart
in your Twig templates.
As a result, for the show
operation, the following Twig variables will be available within the template:
resource
App\Entity\Order
cart
App\Entity\Order
operation
Sylius\Resource\Metadata\Show
resource_metadata
Sylius\Resource\Metadata\ResourceMetadata
app
Symfony\Bridge\Twig\AppVariable
You can override your resource's plural name via the pluralName
parameter of the AsResource
PHP attribute.
In this example, the books
variable is replaced with library
in your Twig templates.
As a result, for the index
operation, the following Twig variables will be available within the template:
resources
Pagerfanta\Pagerfanta
library
Pagerfanta\Pagerfanta
operation
Sylius\Resource\Metadata\Index
resource_metadata
Sylius\Resource\Metadata\ResourceMetadata
app
Symfony\Bridge\Twig\AppVariable
You can define simple variables within the AsResource
attribute via the vars
parameter.
You can then access these variables in your Twig templates. These variables will be available for every operation associated with this resource.