sylius/resource
Sylius Resource is a lightweight foundation for building domain resources in Symfony apps. It provides resource configuration, controllers, repositories, events, and form/grid integration to speed up CRUD and admin tooling while keeping your domain and persistence clean.
Installation Add the package via Composer:
composer require sylius/resource
No additional configuration is required for basic usage.
First Use Case: Defining a Resource
Create a simple resource class (e.g., Post) extending Sylius\Component\Resource\Model\ResourceInterface and implement ResourceInterface:
namespace App\Entity;
use Sylius\Component\Resource\Model\ResourceInterface;
class Post implements ResourceInterface
{
// Your properties (e.g., id, title, content)
}
Registering the Resource
Define a Resource class in config/packages/sylius_resource.yaml:
sylius_resource:
resources:
app.post:
driver: doctrine/orm
classes:
model: App\Entity\Post
Basic CRUD Operations
Use the ResourceRepository and ResourceRegistry services:
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Resource\ResourceRegistryInterface;
class PostController extends AbstractController
{
public function index(ResourceRegistryInterface $registry)
{
$repository = $registry->getRepository('app.post');
$posts = $repository->findAll();
return $this->render('posts/index.html.twig', ['posts' => $posts]);
}
}
sylius_resource:
resources:
app.post:
driver: doctrine/orm
classes:
model: App\Entity\Post
repository: App\Repository\PostRepository
form: App\Form\PostType
Sylius\Component\Resource\Repository\RepositoryInterface for custom logic.$post = $registry->getRepository('app.post')->find(1);
$post = $registry->getFactory('app.post')->createNew();
ResourceFormFactory to generate forms tied to your resource:
$form = $this->createForm(
$registry->getFormType('app.post'),
$post
);
ResourceCreated, ResourceUpdated):
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\ResourceEvents;
$dispatcher->addListener(ResourceEvents::RESOURCE_CREATED, function (ResourceEvent $event) {
$resource = $event->getSubject();
// Custom logic on creation
});
ResourceInterface with API Platform’s ResourceInterface for automatic API generation.JsonSerializable or use Symfony’s serializer with your resource entities.Driver Mismatches
driver in your config matches the actual database layer (e.g., doctrine/orm vs. doctrine/dbal).EntityManager or Connection is properly configured.Circular Dependencies
Post has Author, Author has Post).Repository Overrides
RepositoryInterface; otherwise, they won’t be autowired.Sylius\Component\Resource\Repository\RepositoryInterface:
class PostRepository extends AbstractRepository implements RepositoryInterface
Form Type Conflicts
app.post_form).Event Dispatching
PRE_PERSIST/PRE_UPDATE events.$resources = $registry->getAllResourceNames();
// ['app.post', 'app.author']
$config = $registry->getConfiguration('app.post');
SYLIUS_RESOURCE_DEBUG=true in your .env to log resource-related operations.Custom Drivers
Implement Sylius\Component\Resource\Driver\DriverInterface for non-Doctrine storage (e.g., Elasticsearch, MongoDB).
Dynamic Resource Loading
Use ResourceRegistryInterface::getAllResourceNames() to dynamically load resources based on user roles or other conditions.
Bulk Operations
Combine with Doctrine’s BulkOperations or QueryBuilder for batch updates/deletes:
$qb = $repository->createQueryBuilder('p');
$qb->delete('p', 'p')->where('p.published = :val')->setParameter('val', false);
$qb->getQuery()->execute();
Soft Deletes Extend your resource model to support soft deletes:
use Gedmo\Timestampable\Traits\TimestampableEntity;
class Post implements ResourceInterface
{
use TimestampableEntity;
private ?\DateTimeInterface $deletedAt = null;
}
Then configure Doctrine’s SoftDeleteable listener.
Translation Support
Use gedmo/translatable with Sylius Resource for multilingual fields:
sylius_resource:
resources:
app.post:
classes:
model: App\Entity\Post
translation: App\Entity\PostTranslation
How can I help you explore Laravel packages today?