Sylius Stack
  • Sylius Stack Documentation
  • Getting started
  • Cookbook
    • How to customize your admin panel
      • Basic operations
      • Customizing your grids
      • Customizing the logo
      • Customizing the menu
      • Configuring the security access
      • Customizing the page titles
    • How to use in a DDD architecture
      • Architecture overview
      • Resource configuration
      • Basic operations
      • Operation using a grid
  • Admin UI
    • Getting started
  • Bootstrap Admin UI
    • Getting started
  • Resource
    • Resource Bundle documentation
      • Installation
      • Create new resource
      • Configure your resource
      • Configure your operations
      • Validation
      • Redirect
      • Resource factories
      • Providers
      • Processors
      • Responders
      • Legacy Resource Documentation
        • Configuration
        • Services
        • Routing
        • Forms
        • Getting a Single Resource
        • Getting a Collection of Resources
        • Creating Resources
        • Updating Resources
        • Deleting Resources
        • Configuring a state machine
        • Configuration Reference
  • Grid
    • Grid Bundle documentation
      • Installation
      • Creating your first grid
      • Configuring Fields
      • Field types
      • Creating a custom Field Type
      • Creating a custom Action
      • Creating a custom Bulk Action
      • Filters
      • Creating a custom Filter
      • Advanced configuration
      • Configuration Reference
  • 🍀Twig Extra
    • Getting started
  • 🌱Twig Hooks
    • Getting started
    • Passing data to your hookables
    • Making your hookables configurable
    • Autoprefixing feature
    • Composable Layouts with a predictable structure
    • Advanced
      • Ergonomic work with hooks
      • Metadata objects
      • Multiple hooks inside a single template
      • Overriding hookables
Powered by GitBook
On this page
  • Implement the Resource interface
  • Use the Resource attribute
  • Advanced configuration
  • Configure the resource name
  • Configure the resource plural name
  • Configure additional resource vars
  1. Resource
  2. Resource Bundle documentation

Configure your resource

PreviousCreate new resourceNextConfigure your operations

Last updated 1 month ago

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.

Implement the Resource interface

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.

src/Entity/Book.php

namespace App\Entity;

class Book implements ResourceInterface
{
    public function getId(): int
    {
        return $this->id;
    }
}

Use the Resource attribute

Next, add the #[AsResource] PHP attribute to your Doctrine entity to register it as a Sylius resource.

src/Entity/Book.php
namespace App\Entity;

use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Model\ResourceInterface;

#[AsResource]
class Book implements ResourceInterface
{
}

Run the following command to verify that your resource is correctly configured.

$ 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, the alias for your Sylius resource will be app.book, which combines the application name and the resource name with this format : {application}.{resource}.

Advanced configuration

Configure the resource name

You can override your resource's name via the name parameter of the AsResource PHP attribute.

src/Entity/Order.php
namespace App\Entity;

use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Model\ResourceInterface;

#[AsResource(name: 'cart')]
class Order implements ResourceInterface
{
}

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:

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

Configure the resource plural name

You can override your resource's plural name via the pluralName parameter of the AsResource PHP attribute.

src/Entity/Book.php
namespace App\Entity;

use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Model\ResourceInterface;

#[AsResource(pluralName: 'library')]
class Book implements ResourceInterface
{
}

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:

Name
Type

resources

Pagerfanta\Pagerfanta

library

Pagerfanta\Pagerfanta

operation

Sylius\Resource\Metadata\Index

resource_metadata

Sylius\Resource\Metadata\ResourceMetadata

app

Symfony\Bridge\Twig\AppVariable

Configure additional resource vars

You can define simple variables within the AsResource attribute via the vars parameter.

src/Entity/Book.php
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 then access these variables in your Twig templates. These variables will be available for every operation associated with this resource.

<h1>{{ operation.vars.header }}</h1>
<h2>{{ operation.vars.subheader }}</h2>
create a new resource
Configure your resource
Implement the Resource interface
Use the Resource attribute
Advanced configuration
Configure the resource name
Configure the resource plural name
Configure the resource vars