# Configure your operations

Read the previous chapter to [configure your resource](/resource/index/configure_your_resource.md).

Now, with your fresh new resource, you have to define the operations that you need to implement. There are some basic CRUD operations and more.

* [Configure your operations](#configure-your-operations)
  * [Basic operations](#basic-operations)
    * [Index operation](#index-operation)
    * [Use a grid for your index operation](#use-a-grid-for-your-index-operation)
    * [Show operation](#show-operation)
    * [Create operation](#create-operation)
    * [Update operation](#update-operation)
    * [Delete operation](#delete-operation)
    * [Bulk delete operation](#bulk-delete-operation)
    * [State machine operation](#state-machine-operation)
  * [Advanced configuration](#advanced-configuration)
    * [Configure the path](#configure-the-path)
    * [Configure the short name](#configure-the-short-name)
    * [Configure the templates' dir](#configure-the-templates-dir)
    * [Configure the routes' prefix](#configure-the-routes-prefix)
    * [Configure the section](#configure-the-section)
    * [Configure the resource identifier](#configure-the-resource-identifier)
    * [Configure the vars](#configure-the-vars)

## Basic operations

### Index operation

`Index` operation allows to browse all items of your resource.

{% tabs %}
{% tab title="PHP attributes" %}
{% code title="src/Entity/Book.php" lineNumbers="true" %}

```php
namespace App\Entity;

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

#[AsResource(
    operations: [
        new Index(),
    ],
)]
class Book implements ResourceInterface
{
}
```

{% endcode %}
{% endtab %}

{% tab title="External PHP file" %}
{% code title="config/sylius/resources/book.php" lineNumbers="true" %}

```php
<?php

declare(strict_types=1);

use App\Entity\Book;
use Sylius\Resource\Metadata\Index;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\ResourceMetadata;

return (new ResourceMetadata())
    ->withClass(Book::class)
    ->withOperations(
        new Operations([
            new Index(),
        ])
    )
;
```

{% endcode %}
{% endtab %}
{% endtabs %}

It will configure this route for your `index` operation.

| Name             | Method | Path   |
| ---------------- | ------ | ------ |
| app\_book\_index | GET    | /books |

On your Twig template, these variables are available

| Name               | Type                                      |
| ------------------ | ----------------------------------------- |
| resources          | Pagerfanta\Pagerfanta                     |
| books              | Pagerfanta\Pagerfanta                     |
| operation          | Sylius\Resource\Metadata\Index            |
| resource\_metadata | Sylius\Resource\Metadata\ResourceMetadata |
| app                | Symfony\Bridge\Twig\AppVariable           |

### Use a grid for your index operation

To use a grid for you operation, you need to install the [Sylius grid package](https://github.com/Sylius/SyliusGridBundle/)

{% tabs %}
{% tab title="PHP attributes" %}
{% code title="src/Entity/Book.php" lineNumbers="true" %}

```php
namespace App\Entity;

use App\Grid\BookGrid;
use Sylius\Resource\Model\ResourceInterface;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Metadata\Index;

#[AsResource(
    operations: [
        // You can use either the FQCN of your grid
        new Index(grid: BookGrid::class),
        // Or you can use the grid name
        new Index(grid: 'app_book'),
    ],
)]
class Book implements ResourceInterface
{
}
```

{% endcode %}
{% endtab %}

{% tab title="External PHP file" %}
{% code title="config/sylius/resources/book.php" lineNumbers="true" %}

```php
<?php

declare(strict_types=1);

use App\Entity\Book;
use Sylius\Resource\Metadata\Index;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\ResourceMetadata;

return (new ResourceMetadata())
    ->withClass(Book::class)
    ->withOperations(
        new Operations([
            // You can use either the FQCN of your grid
            new Index(grid: BookGrid::class),
            // Or you can use the grid name
            new Index(grid: 'app_book'),
        ])    
    )
;
```

{% endcode %}
{% endtab %}
{% endtabs %}

On your Twig template, these variables are available

| Name               | Type                                                    |
| ------------------ | ------------------------------------------------------- |
| resources          | Sylius\Bundle\ResourceBundle\Grid\View\ResourceGridView |
| books              | Sylius\Bundle\ResourceBundle\Grid\View\ResourceGridView |
| operation          | Sylius\Resource\Metadata\Index                          |
| resource\_metadata | Sylius\Resource\Metadata\ResourceMetadata               |
| app                | Symfony\Bridge\Twig\AppVariable                         |

The iterator for your books will be available as `books.data` or `resources.data`.

### Show operation

`Show` operation allows to view details of an item.

{% tabs %}
{% tab title="PHP attributes" %}
{% code title="src/Entity/Book.php" lineNumbers="true" %}

```php
namespace App\Entity;

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

#[AsResource(
    operations: [
        new Show(),
    ],
)
class Book implements ResourceInterface
{
}
```

{% endcode %}
{% endtab %}

{% tab title="External PHP file" %}
{% code title="config/sylius/resources/book.php" lineNumbers="true" %}

```php
<?php

declare(strict_types=1);

use App\Entity\Book;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\ResourceMetadata;
use Sylius\Resource\Metadata\Show;

return (new ResourceMetadata())
    ->withClass(Book::class)
    ->withOperations(
        new Operations([
            new Show(),
        ])    
    )
;
```

{% endcode %}
{% endtab %}
{% endtabs %}

It will configure this route for your `show` operation.

| Name            | Method | Path        |
| --------------- | ------ | ----------- |
| app\_book\_show | GET    | /books/{id} |

On your Twig template, these variables are available

| Name               | Type                                      |
| ------------------ | ----------------------------------------- |
| resource           | App\Entity\Book                           |
| book               | App\Entity\Book                           |
| operation          | Sylius\Resource\Metadata\Show             |
| resource\_metadata | Sylius\Resource\Metadata\ResourceMetadata |
| app                | Symfony\Bridge\Twig\AppVariable           |

### Create operation

`Create` operation allows to add a new item of your resource.

{% tabs %}
{% tab title="PHP attributes" %}
{% code title="src/Entity/Book.php" lineNumbers="true" %}

```php
namespace App\Entity;

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

#[AsResource(
    operations: [
        new Create(),
    ],
)
class Book implements ResourceInterface
{
}
```

{% endcode %}
{% endtab %}

{% tab title="External PHP file" %}
{% code title="config/sylius/resources/book.php" lineNumbers="true" %}

```php
<?php

declare(strict_types=1);

use App\Entity\Book;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\ResourceMetadata;

return (new ResourceMetadata())
    ->withClass(Book::class)
    ->withOperations(
        new Operations([
            new Create(),
        ])    
    )
;
```

{% endcode %}
{% endtab %}
{% endtabs %}

It will configure this route for your `create` operation.

| Name              | Method    | Path       |
| ----------------- | --------- | ---------- |
| app\_book\_create | GET, POST | /books/new |

On your Twig template, these variables are available

| Name               | Type                                      |
| ------------------ | ----------------------------------------- |
| resource           | App\Entity\Book                           |
| book               | App\Entity\Book                           |
| operation          | Sylius\Resource\Metadata\Create           |
| resource\_metadata | Sylius\Resource\Metadata\ResourceMetadata |
| app                | Symfony\Bridge\Twig\AppVariable           |

The iterator for your books will be available as `books.data` or `resources.data`.

### Update operation

`Update` operation allows to edit an existing item of your resource.

{% tabs %}
{% tab title="PHP attributes" %}
{% code title="src/Entity/Book.php" lineNumbers="true" %}

```php
namespace App\Entity;

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

#[AsResource(
    operations: [
        new Update(),
    ],
)
class Book implements ResourceInterface
{
}
```

{% endcode %}
{% endtab %}

{% tab title="External PHP file" %}
{% code title="config/sylius/resources/book.php" lineNumbers="true" %}

```php
<?php

declare(strict_types=1);

use App\Entity\Book;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\ResourceMetadata;
use Sylius\Resource\Metadata\Update;

return (new ResourceMetadata())
    ->withClass(Book::class)
    ->withOperations(
        new Operations([
            new Update(),
        ])    
    )
;
```

{% endcode %}
{% endtab %}
{% endtabs %}

It will configure this route for your `update` operation.

| Name              | Method          | Path             |
| ----------------- | --------------- | ---------------- |
| app\_book\_update | GET, PUT, PATCH | /books/{id}/edit |

On your Twig template, these variables are available

| Name               | Type                                      |
| ------------------ | ----------------------------------------- |
| resource           | App\Entity\Book                           |
| book               | App\Entity\Book                           |
| operation          | Sylius\Resource\Metadata\Update           |
| resource\_metadata | Sylius\Resource\Metadata\ResourceMetadata |
| app                | Symfony\Bridge\Twig\AppVariable           |

### Delete operation

`Delete` operation allows to remove an existing item of your resource.

{% tabs %}
{% tab title="PHP attributes" %}
{% code title="src/Entity/Book.php" lineNumbers="true" %}

```php
<?php

declare(strict_types=1);

namespace App\Entity;

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

#[AsResource(
    operations: [
        new Delete(),
    ],
)
class Book implements ResourceInterface
{
}
```

{% endcode %}
{% endtab %}

{% tab title="External PHP file" %}
{% code title="config/sylius/resources/book.php" lineNumbers="true" %}

```php
<?php

declare(strict_types=1);

use App\Entity\Book;
use Sylius\Resource\Metadata\Delete;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\ResourceMetadata;

return (new ResourceMetadata())
    ->withClass(Book::class)
    ->withOperations(
        new Operations([
            new Delete(),
        ])    
    )
;
```

{% endcode %}
{% endtab %}
{% endtabs %}

It will configure this route for your `delete` operation.

| Name              | Method | Path        |
| ----------------- | ------ | ----------- |
| app\_book\_delete | DELETE | /books/{id} |

### Bulk delete operation

`Bulk delete` operation allows to remove several items of your resource at the same time.

{% tabs %}
{% tab title="PHP attributes" %}
{% code title="src/Entity/Book.php" lineNumbers="true" %}

```php
namespace App\Entity;

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

#[AsResource(
    operations: [
        new BulkDelete(),
    ],
)
class Book implements ResourceInterface
{
}
```

{% endcode %}
{% endtab %}

{% tab title="External PHP file" %}
{% code title="config/sylius/resources/book.php" lineNumbers="true" %}

```php
<?php

declare(strict_types=1);

use App\Entity\Book;
use Sylius\Resource\Metadata\BulkDelete;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\ResourceMetadata;

return (new ResourceMetadata())
    ->withClass(Book::class)
    ->withOperations(
        new Operations([
            new BulkDelete(),
        ])
    )
;
```

{% endcode %}
{% endtab %}
{% endtabs %}

It will configure this route for your `bulk_delete` operation.

| Name                    | Method | Path                |
| ----------------------- | ------ | ------------------- |
| app\_book\_bulk\_delete | DELETE | /books/bulk\_delete |

### State machine operation

`State machine` operation allows to apply a transition to an item of your resource.

As an example, we add a `publish` operation to our book resource.

{% tabs %}
{% tab title="PHP attributes" %}
{% code title="src/Entity/Book.php" lineNumbers="true" %}

```php
namespace App\Entity;

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

#[AsResource(
    operations: [
        new ApplyStateMachineTransition(stateMachineTransition: 'publish'),
    ],
)
class Book implements ResourceInterface
{
}
```

{% endcode %}
{% endtab %}

{% tab title="External PHP file" %}
{% code title="config/sylius/resources/book.php" lineNumbers="true" %}

```php
<?php

declare(strict_types=1);

use App\Entity\Book;
use Sylius\Resource\Metadata\ApplyStateMachineTransition;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\ResourceMetadata;

return (new ResourceMetadata())
    ->withClass(Book::class)
    ->withOperations(
        new Operations([
            new ApplyStateMachineTransition(stateMachineTransition: 'publish'),
        ])
    )
;
```

{% endcode %}
{% endtab %}
{% endtabs %}

It will configure this route for your `apply_state_machine_transition` operation.

| Name               | Method | Path                |
| ------------------ | ------ | ------------------- |
| app\_book\_publish | GET    | /books/{id}/publish |

## Advanced configuration

### Configure the path

It customizes the path for your operations.

{% code title="src/Entity/Customer.php" lineNumbers="true" %}

```php
namespace App\Entity;

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

#[AsResource(
    operations: [
        new Create(path: 'register'),
        new Update(path: '{id}/edition'),
    ],
)
class Customer implements ResourceInterface
{
}
```

{% endcode %}

{% code title="config/sylius/resources/customer.php" lineNumbers="true" %}

```php
<?php

declare(strict_types=1);

use App\Entity\Customer;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\ResourceMetadata;
use Sylius\Resource\Metadata\Update;

return (new ResourceMetadata())
    ->withClass(Customer::class)
    ->withOperations(
        new Operations([
            new Create(path: 'register'),
            new Update(path: '{id}/edition'),
        ])
    )
;
```

{% endcode %}

| Name              | Method    | Path                |
| ----------------- | --------- | ------------------- |
| app\_book\_create | GET, POST | /books/register     |
| app\_book\_update | GET, POST | /books/{id}/edition |

### Configure the short name

It customizes the path for your operations.

{% code title="src/Entity/Customer.php" lineNumbers="true" %}

```php
namespace App\Entity;

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

#[AsResource(
    operations: [
        new Create(shortName: 'register'),
    ],
)
class Customer implements ResourceInterface
{
}
```

{% endcode %}

{% code title="config/sylius/resources/customer.php" lineNumbers="true" %}

```php
<?php

declare(strict_types=1);

use App\Entity\Customer;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\ResourceMetadata;

return (new ResourceMetadata())
    ->withClass(Customer::class)
    ->withOperations(
        new Operations([
            new Create(shortName: 'register'),
        ])
    )
;
```

{% endcode %}

| Name                | Method    | Path            |
| ------------------- | --------- | --------------- |
| app\_book\_register | GET, POST | /books/register |

It influences the path by default too, but you can still customize the path if needed.

### Configure the templates' dir

It defines the templates directory for your operations.

As an example, we defines `index`, `create`, `update` and `show` operations to our book resource.

{% code title="src/Entity/Book.php" lineNumbers="true" %}

```php
namespace App\Entity;

use Sylius\Resource\Model\ResourceInterface;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Metadata\Index;
use Sylius\Resource\Metadata\Show;
use Sylius\Resource\Metadata\Update;

#[AsResource(
    templatesDir: 'book',
    operations: [
        new Index(),
        new Create(),
        new Update(),
        new Show(),
    ],
)
class Book implements ResourceInterface
{
}
```

{% endcode %}

{% code title="config/sylius/resources/book.php" lineNumbers="true" %}

```php
<?php

declare(strict_types=1);

use App\Entity\Book;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Metadata\Index;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\ResourceMetadata;
use Sylius\Resource\Metadata\Show;
use Sylius\Resource\Metadata\Update;

return (new ResourceMetadata())
    ->withClass(Book::class)
    ->withTemplatesDir('book')
    ->withOperations(
        new Operations([
            new Index(),
            new Create(),
            new Update(),
            new Show(),
        ])
    )
;
```

{% endcode %}

| Operation | Template Path                    |
| --------- | -------------------------------- |
| index     | templates/books/index.html.twig  |
| create    | templates/books/create.html.twig |
| update    | templates/books/update.html.twig |
| show      | templates/books/show\.html.twig  |

{% hint style="info" %}
You can use `@SyliusAdminUi/crud` as templates dir from the [sylius/admin-ui](/admin-ui/getting-started.md) package.
{% endhint %}

| Operation | Template Path                        |
| --------- | ------------------------------------ |
| index     | @SyliusAdminUi/crud/index.html.twig  |
| create    | @SyliusAdminUi/crud/create.html.twig |
| update    | @SyliusAdminUi/crud/update.html.twig |
| show      | @SyliusAdminUi/crud/show\.html.twig  |

### Configure the routes' prefix

It adds a prefix to the path for each operation.

{% code title="src/Entity/Book.php" lineNumbers="true" %}

```php
namespace App\Entity;

use Sylius\Resource\Model\ResourceInterface;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Metadata\BulkDelete;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Metadata\Delete;
use Sylius\Resource\Metadata\Index;
use Sylius\Resource\Metadata\Show;
use Sylius\Resource\Metadata\Update;

#[AsResource(
    routePrefix: '/admin',
    operations: [
        new Index(routePrefix: ''),  // you can also customize the route prefix at the operation level too for extra flexibility
        new Create(),
        new Update(),
        new Delete(),
        new BulkDelete(),
        new Show(),
    ],
)
class Book implements ResourceInterface
{
}
```

{% endcode %}

{% code title="config/sylius/resources/book.php" lineNumbers="true" %}

```php
<?php

declare(strict_types=1);

use App\Entity\Book;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Metadata\Index;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\ResourceMetadata;
use Sylius\Resource\Metadata\Show;
use Sylius\Resource\Metadata\Update;

return (new ResourceMetadata())
    ->withClass(Book::class)
    ->withRoutePrefix('/admin')
    ->withOperations(
        new Operations([
            new Index(routePrefix: ''),  // you can also customize the route prefix at the operation level too for extra flexibility
            new Create(),
            new Update(),
            new Show(),
        ])
    )
;
```

{% endcode %}

| Name                    | Method          | Path                      |
| ----------------------- | --------------- | ------------------------- |
| app\_book\_index        | GET             | /books/                   |
| app\_book\_create       | GET, POST       | /admin/books/new          |
| app\_book\_update       | GET, PUT, PATCH | /admin/books/{id}/edit    |
| app\_book\_delete       | DELETE          | /admin/books/{id}         |
| app\_book\_bulk\_delete | DELETE          | /admin/books/bulk\_delete |
| app\_book\_show         | GET             | /admin/books/{id}         |

### Configure the routes' name

It customizes the route name for individual operations.

{% code title="src/Entity/Book.php" lineNumbers="true" %}

```php
namespace App\Entity;

use Sylius\Resource\Model\ResourceInterface;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Metadata\BulkDelete;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Metadata\Delete;
use Sylius\Resource\Metadata\Index;
use Sylius\Resource\Metadata\Show;
use Sylius\Resource\Metadata\Update;

#[AsResource(
    operations: [
        new Index(routeName: 'library_book_list'),
        new Create(routeName: 'library_book_add'),
        new Update(),
        new Delete(),
        new BulkDelete(),
        new Show(),
    ],
)
class Book implements ResourceInterface
{
}
```

{% endcode %}

{% code title="config/sylius/resources/book.php" lineNumbers="true" %}

```php
<?php

declare(strict_types=1);

use App\Entity\Book;
use Sylius\Resource\Metadata\BulkDelete;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Metadata\Delete;
use Sylius\Resource\Metadata\Index;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\ResourceMetadata;
use Sylius\Resource\Metadata\Show;
use Sylius\Resource\Metadata\Update;

return (new ResourceMetadata())
    ->withClass(Book::class)
    ->withOperations(
        new Operations([
            new Index(routeName: 'library_book_list'),
            new Create(routeName: 'library_book_add'),
            new Update(),
            new Delete(),
            new BulkDelete(),
            new Show(),            
        ])
    )
;
```

{% endcode %}

| Name                    | Method          | Path                |
| ----------------------- | --------------- | ------------------- |
| library\_book\_list     | GET             | /books/             |
| library\_book\_add      | GET, POST       | /books/new          |
| app\_book\_update       | GET, PUT, PATCH | /books/{id}/edit    |
| app\_book\_delete       | DELETE          | /books/{id}         |
| app\_book\_bulk\_delete | DELETE          | /books/bulk\_delete |
| app\_book\_show         | GET             | /books/{id}         |

### Configure the section

It changes the route name for each operation.

{% code title="src/Entity/Book.php" lineNumbers="true" %}

```php
namespace App\Entity;

use Sylius\Resource\Model\ResourceInterface;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Metadata\BulkDelete;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Metadata\Delete;
use Sylius\Resource\Metadata\Index;
use Sylius\Resource\Metadata\Show;
use Sylius\Resource\Metadata\Update;

#[AsResource(
    section: 'admin',
    routePrefix: '/admin',
    operations: [
        new Index(),
        new Create(),
        new Update(),
        new Delete(),
        new BulkDelete(),
    ],
)
#[AsResource(
    section: 'shop',
    operations: [
        new Index(),
        new Show(),
    ],
)
class Book implements ResourceInterface
{
}
```

{% endcode %}

{% code title="config/sylius/resources/admin/book.php" lineNumbers="true" %}

```php
<?php

declare(strict_types=1);

use App\Entity\Book;
use Sylius\Resource\Metadata\BulkDelete;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Metadata\Delete;
use Sylius\Resource\Metadata\Index;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\ResourceMetadata;
use Sylius\Resource\Metadata\Show;
use Sylius\Resource\Metadata\Update;

return (new ResourceMetadata())
    ->withClass(Book::class)
    ->withSection('admin')
    ->withRoutePrefix('/admin')
    ->withOperations(
        new Operations([
            new Index(),
            new Create(),
            new Update(),
            new Delete(),
            new BulkDelete(),
        ])
    )
;
```

{% endcode %}

{% code title="config/sylius/resources/shop/book.php" lineNumbers="true" %}

```php
use App\Entity\Book;
use Sylius\Resource\Metadata\BulkDelete;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Metadata\Delete;
use Sylius\Resource\Metadata\Index;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\ResourceMetadata;
use Sylius\Resource\Metadata\Show;
use Sylius\Resource\Metadata\Update;

return (new ResourceMetadata())
    ->withClass(Book::class)
    ->withSection('shop')
    ->withOperations(
        new Operations([
            new Index(),
            new Show(),
        ])
    )
;
```

{% endcode %}

| Name                           | Method          | Path                      |
| ------------------------------ | --------------- | ------------------------- |
| app\_admin\_book\_index        | GET             | /admin/books/             |
| app\_admin\_book\_create       | GET, POST       | /admin/books/new          |
| app\_admin\_book\_update       | GET, PUT, PATCH | /admin/books/{id}/edit    |
| app\_admin\_book\_delete       | DELETE          | /admin/books/{id}         |
| app\_admin\_book\_bulk\_delete | DELETE          | /admin/books/bulk\_delete |
| app\_shop\_book\_index         | GET             | /books/                   |
| app\_shop\_book\_show          | GET             | /books/{id}               |

### Configure the resource identifier

It changes the resource identifier for each operation.

{% code title="src/Entity/Book.php" lineNumbers="true" %}

```php
namespace App\Entity;

use Sylius\Resource\Model\ResourceInterface;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Metadata\BulkDelete;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Metadata\Delete;
use Sylius\Resource\Metadata\Index;
use Sylius\Resource\Metadata\Update;

#[AsResource(
    identifier: 'code',
    operations: [
        new Index(),
        new Create(),
        new Update(),
        new Delete(),
        new BulkDelete(),
    ],
)
class Book implements ResourceInterface
{
}
```

{% endcode %}

{% code title="config/sylius/resources/book.php" lineNumbers="true" %}

```php
<?php

declare(strict_types=1);

use App\Entity\Book;
use Sylius\Resource\Metadata\BulkDelete;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Metadata\Delete;
use Sylius\Resource\Metadata\Index;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\ResourceMetadata;
use Sylius\Resource\Metadata\Update;

return (new ResourceMetadata())
    ->withClass(Book::class)
    ->withIdentifier('code')
    ->withOperations(
        new Operations([
            new Index(),
            new Create(),
            new Update(),
            new Delete(),
            new BulkDelete(),          
        ])
    )
;
```

{% endcode %}

| Name                    | Method          | Path                      |
| ----------------------- | --------------- | ------------------------- |
| app\_book\_index        | GET             | /admin/books/             |
| app\_book\_create       | GET, POST       | /admin/books/new          |
| app\_book\_update       | GET, PUT, PATCH | /admin/books/{code}/edit  |
| app\_book\_delete       | DELETE          | /admin/books/{code}       |
| app\_book\_bulk\_delete | DELETE          | /admin/books/bulk\_delete |

### Configure the vars

It defines the simple vars that you can use on your templates.

{% code title="src/Entity/Book.php" lineNumbers="true" %}

```php
namespace App\Entity;

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

#[AsResource(
    vars: [
        'header' => 'Library', 
        'subheader' => 'Managing your library',
    ],
    operations: [
        new Create(vars: [
            'subheader' => 'Adding a book',
        ]),
    ],
)
class Book implements ResourceInterface
{
}
```

{% endcode %}

{% code title="config/sylius/resources/book.php" lineNumbers="true" %}

```php
<?php

declare(strict_types=1);

use App\Entity\Book;
use Sylius\Resource\Metadata\Create;
use Sylius\Resource\Metadata\Operations;
use Sylius\Resource\Metadata\ResourceMetadata;

return (new ResourceMetadata())
    ->withClass(Book::class)
    ->withVars([
        'header' => 'Library', 
        'subheader' => 'Managing your library',
    ])
    ->withOperations(
        new Operations([
            new Create(vars: [
                'subheader' => 'Adding a book',
            ]),         
        ])
    )
;
```

{% endcode %}

You can use these vars on your Twig templates. These vars will be available on any operations for this resource.

```html
<h1>{{ operation.vars.header }}<!-- Library --></h1>
<h2>{{ operation.vars.subheader }}<!-- Adding a book --></h2>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://stack.sylius.com/resource/index/configure_your_operations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
