Read the previous chapter to create a new resource.
To declare your resource as a Sylius one, you need to implement
the Sylius\Component\Resource\Model\ResourceInterface
which requires you to implement a getId()
method.
// src/Entity/Book.php
namespace App\Entity;
class Book implements ResourceInterface
{
public function getId(): int
{
return $this->id;
}
}
We add the PHP attribute #[Resource]
to the Doctrine entity.
It will configure your entity as a Sylius resource.
namespace App\Entity;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Model\ResourceInterface;
#[AsResource]
class Book implements ResourceInterface
{
}
$ bin/console sylius:debug:resource 'App\Entity\book'
+--------------------+------------------------------------------------------------+
| name | book |
| application | app |
| driver | doctrine/orm |
| classes.model | App\Entity\Book |
| classes.controller | Sylius\Bundle\ResourceBundle\Controller\ResourceController |
| classes.factory | Sylius\Component\Resource\Factory\Factory |
| classes.form | Sylius\Bundle\ResourceBundle\Form\Type\DefaultResourceType |
+--------------------+------------------------------------------------------------+
By default, it will have the app.book
alias in Sylius resource which is a concatenation of the application name and
the resource name {application}.{name}
.
It defines the resource name.
namespace App\Entity;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Model\ResourceInterface;
#[AsResource(name: 'cart')]
class Order implements ResourceInterface
{
}
On your Twig templates, the order
variable will be replaced by the cart
one.
As an example, on a show
operation following Twig variables will be available:
Name | Type |
---|---|
resource | App\Entity\Order |
cart | App\Entity\Order |
operation | Sylius\Resource\Metadata\Show |
resource_metadata | Sylius\Resource\Metadata\ResourceMetadata |
app | Symfony\Bridge\Twig\AppVariable |
It defines the resource plural name.
namespace App\Entity;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Model\ResourceInterface;
#[AsResource(pluralName: 'library')]
class Book implements ResourceInterface
{
}
On your Twig templates, the books
variable will be replaced by the library
one.
As an example, on an index
operation these Twig variables will be available:
Name | Type |
---|---|
resources | Pagerfanta\Pagerfanta |
library | Pagerfanta\Pagerfanta |
operation | Sylius\Resource\Metadata\Index |
resource_metadata | Sylius\Resource\Metadata\ResourceMetadata |
app | Symfony\Bridge\Twig\AppVariable |
It defines the simple vars that you can use on your templates.
namespace App\Entity;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Model\ResourceInterface;
#[AsResource(vars: ['header' => 'Library', 'subheader' => 'Managing your library'])]
class Book implements ResourceInterface
{
}
You can use these vars on your Twig templates. These vars will be available on any operations for this resource.
<h1>{{ operation.vars.header }}</h1>
<h2>{{ operation.vars.subheader }}</h2>