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 ResourceInterface in your model class.
  • Configure the class as a resource.
  • You can also configure several doctrine drivers.
  • Update the resource repository
  • Generate API routing.
  • Generate web routing.
  1. Resource
  2. Resource Bundle documentation
  3. Legacy Resource Documentation

Configuration

PreviousLegacy Resource DocumentationNextServices

Last updated 3 months ago

This section is deprecated. However, as of now, the Sylius E-Commerce project is still resorting to this configuration so you might want to check it out.

Now you need to configure your first resource. Let's assume you have a Book entity in your application and it has simple fields:

  • id

  • title

  • author

  • description

You can see a full exemplary configuration of a typical resource

Implement the ResourceInterface in your model class.

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

use Sylius\Resource\Model\ResourceInterface;

class Book implements ResourceInterface
{
    // Most of the time you have the code below already in your class.
    protected $id;

    public function getId()
    {
        return $this->id;
    }
}

Configure the class as a resource.

In your config/packages/sylius_resource.yaml add:

config/packages/sylius_resource.yaml
sylius_resource:
    resources:
        app.book:
            classes:
                model: App\Entity\Book

That's it! Your Book entity is now registered as Sylius Resource.

You can also configure several doctrine drivers.

Remember that the doctrine/orm driver is used by default.

config/packages/sylius_resource.yaml
sylius_resource:
    drivers:
        - doctrine/orm
        - doctrine/phpcr-odm
    resources:
        app.book:
            classes:
                model: App\Entity\Book
        app.article:
            driver: doctrine/phpcr-odm
            classes:
                model: App\Document\ArticleDocument

Update the resource repository

If you use the "make:entity" command you should have a generated repository which extends ServiceEntityRepository. Then you just have to implement SyliusRepositoryInterface and use ResourceRepositoryTrait.

src/Repository/BookRepository.php
namespace App\Repository;

use App\Entity\Book;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Sylius\Resource\Doctrine\Persistence\RepositoryInterface;

class BookRepository extends ServiceEntityRepository implements RepositoryInterface
{
    use ResourceRepositoryTrait;

    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Book::class);
    }
}

And configure this repository class:

config/packages/sylius_resource.yaml
sylius_resource:
    drivers:
        - doctrine/orm
        - doctrine/phpcr-odm
    resources:
        app.book:
            classes:
                model: App\Entity\Book
                repository: App\Entity\BookRepository

Generate API routing.

Add the following lines to config/routes.yaml:

config/routes.yaml
app_book:
    resource: |
        alias: app.book
    type: sylius.resource_api

After that a full JSON/XML CRUD API is ready to use. Sounds crazy? Spin up the built-in server and give it a try:

php bin/console server:run

You should see something like:

Server running on http://127.0.0.1:8000

Quit the server with CONTROL-C.

Now, in a separate Terminal window, call these commands:

curl -i -X POST -H "Content-Type: application/json" -d '{"title": "Lord of The Rings", "author": "J. R. R. Tolkien", "description": "Amazing!"}' http://localhost:8000/books/
curl -i -X GET -H "Accept: application/json" http://localhost:8000/books/

As you can guess, other CRUD actions are available through this API.

Generate web routing.

What if you want to render HTML pages? That's easy! Update the routing configuration:

config/routes.yaml
app_book:
    resource: |
        alias: app.book
    type: sylius.resource

This will generate routing for HTML views.

Run the debug:router command to see available routes:

php bin/console debug:router
------------------------ --------------- -------- ------ -------------------------
Name                     Method          Scheme   Host   Path
------------------------ --------------- -------- ------ -------------------------
app_book_show            GET             ANY      ANY    /books/{id}
app_book_index           GET             ANY      ANY    /books/
app_book_create          GET|POST        ANY      ANY    /books/new
app_book_update          GET|PUT|PATCH   ANY      ANY    /books/{id}/edit
app_book_delete          DELETE          ANY      ANY    /books/{id}

You can configure more options for the routing generation but you can also define each route manually to have it fully configurable. Continue reading to learn more!

Learn more about using Sylius REST API in these articles:

Do you need views for your newly created entity? Read more about , which are a separate bundle of Sylius, but may be very useful for views generation.

How to add a custom model?
How to use Sylius API? - Cookbook
Grids
Go back to the documentation's index