From ea3a09e9846deab30964c894b37eabaaf67cd019 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20THEROUX?= Date: Mon, 17 Feb 2020 16:54:56 +0100 Subject: [PATCH 1/2] Add details on how to extends views This detail would have helped me if it were here --- doc/Cookbook.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/doc/Cookbook.md b/doc/Cookbook.md index dfdc0e4f1..158dd1fc2 100644 --- a/doc/Cookbook.md +++ b/doc/Cookbook.md @@ -134,6 +134,15 @@ sylius_shop_api: total: \NiceTotalView ``` +You have to extend the base view class in order to match view factory type hint. +```php +final class NiceTotalView extends TotalView +{ + /** @var float */ + protected $nicePersonDiscount; +} +``` + One other thing to keep in mind when working with the API and creating new views is that the endpoints that return multiple resources (for example a list of products) are usually paginated. Pagination in the shop api is implemented with the plugin [Pagerfanta](https://github.com/whiteoctober/Pagerfanta) which needs besides the attributes that it should serialize on the product also a key relations like the route to the individual products. The current object you are serializing is in the object "variable" and can be used with the expression syntax as shown below: ```yml relations: From ca191896d1cd59802609f4e32890b93e2fe62c5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20THEROUX?= Date: Mon, 17 Feb 2020 17:04:15 +0100 Subject: [PATCH 2/2] Fix property visibility and class extensibility --- doc/Cookbook.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/Cookbook.md b/doc/Cookbook.md index 158dd1fc2..cf779548a 100644 --- a/doc/Cookbook.md +++ b/doc/Cookbook.md @@ -136,10 +136,10 @@ sylius_shop_api: You have to extend the base view class in order to match view factory type hint. ```php -final class NiceTotalView extends TotalView +class NiceTotalView extends TotalView { /** @var float */ - protected $nicePersonDiscount; + public $nicePersonDiscount; } ```