Skip to content

Commit

Permalink
[DOC] Resource documentation from SyliusResourceBundle repository (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 authored Dec 4, 2024
2 parents 197e8fc + 1797fea commit 77c73fc
Show file tree
Hide file tree
Showing 24 changed files with 3,955 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The Sylius stack is a set of tools for your Symfony projects:

* [**AdminUi:** Minimalist generic templates for your admin panels](admin-ui/getting-started.md)
* [**BootstrapAdminUi:** Build your Bootstrap admin panels with Sylius and Symfony UX](bootstrap-admin-ui/getting-started.md)
* [**ResourceBundle:** Resource management system, routing and CRUD operations](https://github.com/Sylius/SyliusResourceBundle/blob/1.13/docs/index.md)
* [**ResourceBundle:** Resource management system, routing and CRUD operations](resource/index.md)
* [**GridBundle:** Amazing grids with support of filters and custom fields integrated into Symfony](grid/index.md)
* [**TwigExtra:** Additional Twig extensions for your Symfony projects](twig-extra/getting-started.md)
* [**TwigHooks:** Composable Twig layouts](twig-hooks/getting-started.md)
Expand Down
27 changes: 26 additions & 1 deletion docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,34 @@
* [Getting started](bootstrap-admin-ui/getting-started.md)
* [Customizing](bootstrap-admin-ui/customizing.md)

## Resource

* [Resource Bundle documentation](resource/index.md)
* [Configuration](resource/configuration.md)
* [Configure your operations](resource/configure_your_operations.md)
* [Configure your resource](resource/configure_your_resource.md)
* [Create new resource](resource/create_new_resource.md)
* [Create resource](resource/create_resource.md)
* [Delete resource](resource/delete_resource.md)
* [Forms](resource/forms.md)
* [Index resources](resource/index_resources.md)
* [Installation](resource/installation.md)
* [Processors](resource/processors.md)
* [Providers](resource/providers.md)
* [Redirect](resource/redirect.md)
* [Reference](resource/reference.md)
* [Resource factories](resource/resource_factories.md)
* [Responders](resource/responders.md)
* [Routing](resource/routing.md)
* [Services](resource/services.md)
* [Show resource](resource/show_resource.md)
* [State machine](resource/state_machine.md)
* [Update resource](resource/update_resource.md)
* [Validation](resource/validation.md)

## Grid

* [Documentation](grid/index.md)
* [Grid Bundle documentation](grid/index.md)
* [Custom bulk action](grid/custom_bulk_action.md)
* [Installation](grid/installation.md)
* [Field types](grid/field_types.md)
Expand Down
171 changes: 171 additions & 0 deletions docs/resource/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# Configuring Your Resources

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?](https://docs.sylius.com/en/latest/cookbook/entities/custom-model.html)

## Implement the ResourceInterface in your model class.

```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:

```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.
```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`.

```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:

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

## Generate API routing.

Learn more about using Sylius REST API in these articles:
[How to use Sylius API? - Cookbook](https://docs.sylius.com/en/latest/cookbook/api/api.html)

Add the following lines to ``config/routes.yaml``:

```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:

```bash
php bin/console server:run
```
You should see something like:

```bash
Server running on http://127.0.0.1:8000
Quit the server with CONTROL-C.
```
Now, in a separate Terminal window, call these commands:

```bash
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:

```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:

```bash
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}
```
Do you need **views** for your newly created entity? Read more about
[Grids](https://docs.sylius.com/en/latest/components_and_bundles/bundles/SyliusGridBundle/index.html),
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](index.md)**
Loading

0 comments on commit 77c73fc

Please sign in to comment.