Configuration

circle-exclamation

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 How to add a custom model?arrow-up-right

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:

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.

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.

And configure this repository class:

Generate API routing.

Learn more about using Sylius REST API in these articles: How to use Sylius API? - Cookbookarrow-up-right

Add the following lines to config/routes.yaml:

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:

You should see something like:

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

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:

This will generate routing for HTML views.

Run the debug:router command to see available routes:

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

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!

Go back to the documentation's index

Last updated

Was this helpful?