Resource Layer
We created an abstraction on top of Doctrine, to have a consistent and flexible way to manage all the resources. By “resource” we understand every model in the application. The simplest examples of Sylius resources are “product”, “order”, “tax_category”, “promotion”, “user”, “shipping_method” and so on…
There are two types of resources in Sylius:
registered by default - their names begin with
sylius.*
for example:sylius.product
custom resources, from your application which have a separate convention. We place them under
sylius_resource:
resource_name:
in theconfig.yml
. For these, we recommend using the naming convention ofapp.*
for instanceapp.my_entity
.
Sylius resource management system lives in the SyliusResourceBundle and can be used in any Symfony project.
Services
For every resource you have four essential services available:
Factory
Manager
Repository
Controller
Let us take the “product” resource as an example. By default, it is represented by an object of a class that implements the Sylius\Component\Core\Model\ProductInterface
.
Factory
The factory service gives you the ability to create new default objects. It can be accessed via the sylius.factory.product.id
(for the Product resource of course).
Creating resources via this factory method makes the code more testable, and allows you to change the model class easily.
Manager
The manager service is just an alias to appropriate Doctrine’s ObjectManager and can be accessed via the sylius.manager.product.id
. API is the same and you are probably already familiar with it:
Repository
A repository is defined as a service for every resource and shares the API with the standard Doctrine ObjectRepository
. It contains two additional methods for creating a new object instance and a paginator provider.
The repository service is available via the sylius.repository.product.id
and can be used like all the repositories you have seen before.
An important feature of the repositories are theadd($resource)
and theremove($resource)
methods, which take a resource as an argument and perform the adding/removing action with a flush inside.
These actions can be used when the performance of operations is negligible. If you want to perform operations on large sets of data we recommend using the manager instead.
Every Sylius repository supports paginating resources. To create a Pagerfanta instance use the createPaginator
method:
Paginator can be created for specific criteria and with desired sorting:
Controller
This service is the most important for every resource and provides a format-agnostic CRUD controller with the following actions:
[GET] showAction()
for getting a single resource[GET] indexAction()
for retrieving a collection of resources[GET/POST] createAction()
for creating a new resource[GET/PUT] updateAction()
for updating an existing resource[DELETE] deleteAction()
for removing an existing resource
As you see, these actions match the common operations in any REST API and yes, they are format agnostic. This means all Sylius controllers can serve HTML, JSON, or XML, depending on your request.
Additionally, all these actions are very flexible and allow you to use different templates, forms, and repository methods per route. The bundle is very powerful and allows you to register your own resources as well. Here are some examples to give you some idea of what is possible!
Displaying a resource with a custom template and repository methods:
Creating a product using a custom form and a redirection method:
All other methods have the same level of flexibility and are documented in the SyliusResourceBundle.
Last updated