+
+
+
diff --git a/composer.json b/composer.json
index 9180c471..dd975b40 100644
--- a/composer.json
+++ b/composer.json
@@ -4,6 +4,7 @@
"require": {
"php": "^7.0.8",
"ext-iconv": "*",
+ "friendsofsymfony/rest-bundle": "^2.3",
"sensio/framework-extra-bundle": "^5.1",
"sensiolabs/security-checker": "^4.1",
"symfony/asset": "^3.4",
diff --git a/composer.lock b/composer.lock
index 67b7f162..512be1d8 100644
--- a/composer.lock
+++ b/composer.lock
@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
- "content-hash": "fb1fa43df94930a543d8c27c9b62c4ee",
+ "content-hash": "0f9764d69758baf439682a056b7455fe",
"packages": [
{
"name": "composer/ca-bundle",
@@ -1131,6 +1131,108 @@
],
"time": "2016-10-17T18:31:11+00:00"
},
+ {
+ "name": "friendsofsymfony/rest-bundle",
+ "version": "2.3.1",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/FriendsOfSymfony/FOSRestBundle.git",
+ "reference": "1abdf3d82502ac67b93c7f84c844fa147f0ec70e"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/FriendsOfSymfony/FOSRestBundle/zipball/1abdf3d82502ac67b93c7f84c844fa147f0ec70e",
+ "reference": "1abdf3d82502ac67b93c7f84c844fa147f0ec70e",
+ "shasum": ""
+ },
+ "require": {
+ "doctrine/inflector": "^1.0",
+ "php": "^5.5.9|~7.0",
+ "psr/log": "^1.0",
+ "symfony/config": "^2.7|^3.0|^4.0",
+ "symfony/debug": "^2.7|^3.0|^4.0",
+ "symfony/dependency-injection": "^2.7|^3.0|^4.0",
+ "symfony/event-dispatcher": "^2.7|^3.0|^4.0",
+ "symfony/finder": "^2.7|^3.0|^4.0",
+ "symfony/framework-bundle": "^2.7|^3.0|^4.0",
+ "symfony/http-foundation": "^2.7|^3.0|^4.0",
+ "symfony/http-kernel": "^2.7|^3.0|^4.0",
+ "symfony/routing": "^2.7|^3.0|^4.0",
+ "symfony/security-core": "^2.7|^3.0|^4.0",
+ "symfony/templating": "^2.7|^3.0|^4.0",
+ "willdurand/jsonp-callback-validator": "^1.0",
+ "willdurand/negotiation": "^2.0"
+ },
+ "conflict": {
+ "jms/serializer": "1.3.0",
+ "jms/serializer-bundle": "<1.2.0",
+ "sensio/framework-extra-bundle": "<3.0.13"
+ },
+ "require-dev": {
+ "jms/serializer-bundle": "^1.2|^2.0",
+ "phpoption/phpoption": "^1.1",
+ "psr/http-message": "^1.0",
+ "sensio/framework-extra-bundle": "^3.0.13|^4.0|^5.0",
+ "symfony/asset": "^2.7|^3.0|^4.0",
+ "symfony/browser-kit": "^2.7|^3.0|^4.0",
+ "symfony/css-selector": "^2.7|^3.0|^4.0",
+ "symfony/dependency-injection": "^2.7|^3.0|^4.0",
+ "symfony/expression-language": "~2.7|^3.0|^4.0",
+ "symfony/form": "^2.7|^3.0|^4.0",
+ "symfony/phpunit-bridge": "^3.2|^4.0",
+ "symfony/security-bundle": "^2.7|^3.0|^4.0",
+ "symfony/serializer": "^2.7.11|^3.0.4|^4.0",
+ "symfony/twig-bundle": "^2.7|^3.0|^4.0",
+ "symfony/validator": "^2.7|^3.0|^4.0",
+ "symfony/web-profiler-bundle": "^2.7|^3.0|^4.0",
+ "symfony/yaml": "^2.7|^3.0|^4.0"
+ },
+ "suggest": {
+ "jms/serializer-bundle": "Add support for advanced serialization capabilities, recommended, requires ^1.0",
+ "sensio/framework-extra-bundle": "Add support for route annotations and the view response listener, requires ^3.0",
+ "symfony/expression-language": "Add support for using the expression language in the routing, requires ^2.7|^3.0",
+ "symfony/serializer": "Add support for basic serialization capabilities and xml decoding, requires ^2.7|^3.0",
+ "symfony/validator": "Add support for validation capabilities in the ParamFetcher, requires ^2.7|^3.0"
+ },
+ "type": "symfony-bundle",
+ "extra": {
+ "branch-alias": {
+ "dev-master": "2.3-dev"
+ }
+ },
+ "autoload": {
+ "psr-4": {
+ "FOS\\RestBundle\\": ""
+ },
+ "exclude-from-classmap": [
+ "Tests/"
+ ]
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "Lukas Kahwe Smith",
+ "email": "smith@pooteeweet.org"
+ },
+ {
+ "name": "FriendsOfSymfony Community",
+ "homepage": "https://github.com/friendsofsymfony/FOSRestBundle/contributors"
+ },
+ {
+ "name": "Konstantin Kudryashov",
+ "email": "ever.zet@gmail.com"
+ }
+ ],
+ "description": "This Bundle provides various tools to rapidly develop RESTful API's with Symfony",
+ "homepage": "http://friendsofsymfony.github.com",
+ "keywords": [
+ "rest"
+ ],
+ "time": "2018-02-28T13:57:04+00:00"
+ },
{
"name": "jdorn/sql-formatter",
"version": "v1.2.17",
@@ -4500,6 +4602,61 @@
"homepage": "http://symfony.com",
"time": "2018-03-08T16:39:26+00:00"
},
+ {
+ "name": "symfony/templating",
+ "version": "v3.4.6",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/symfony/templating.git",
+ "reference": "6ac0bb6dea36053b97027dae0bf3888e432c1f74"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/symfony/templating/zipball/6ac0bb6dea36053b97027dae0bf3888e432c1f74",
+ "reference": "6ac0bb6dea36053b97027dae0bf3888e432c1f74",
+ "shasum": ""
+ },
+ "require": {
+ "php": "^5.5.9|>=7.0.8"
+ },
+ "require-dev": {
+ "psr/log": "~1.0"
+ },
+ "suggest": {
+ "psr/log": "For using debug logging in loaders"
+ },
+ "type": "library",
+ "extra": {
+ "branch-alias": {
+ "dev-master": "3.4-dev"
+ }
+ },
+ "autoload": {
+ "psr-4": {
+ "Symfony\\Component\\Templating\\": ""
+ },
+ "exclude-from-classmap": [
+ "/Tests/"
+ ]
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "Fabien Potencier",
+ "email": "fabien@symfony.com"
+ },
+ {
+ "name": "Symfony Community",
+ "homepage": "https://symfony.com/contributors"
+ }
+ ],
+ "description": "Symfony Templating Component",
+ "homepage": "https://symfony.com",
+ "time": "2018-01-03T07:37:34+00:00"
+ },
{
"name": "symfony/translation",
"version": "v3.4.6",
@@ -5224,6 +5381,98 @@
],
"time": "2018-01-29T19:49:41+00:00"
},
+ {
+ "name": "willdurand/jsonp-callback-validator",
+ "version": "v1.1.0",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/willdurand/JsonpCallbackValidator.git",
+ "reference": "1a7d388bb521959e612ef50c5c7b1691b097e909"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/willdurand/JsonpCallbackValidator/zipball/1a7d388bb521959e612ef50c5c7b1691b097e909",
+ "reference": "1a7d388bb521959e612ef50c5c7b1691b097e909",
+ "shasum": ""
+ },
+ "require": {
+ "php": ">=5.3.0"
+ },
+ "require-dev": {
+ "phpunit/phpunit": "~3.7"
+ },
+ "type": "library",
+ "autoload": {
+ "psr-0": {
+ "JsonpCallbackValidator": "src/"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "William Durand",
+ "email": "william.durand1@gmail.com",
+ "homepage": "http://www.willdurand.fr"
+ }
+ ],
+ "description": "JSONP callback validator.",
+ "time": "2014-01-20T22:35:06+00:00"
+ },
+ {
+ "name": "willdurand/negotiation",
+ "version": "v2.3.1",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/willdurand/Negotiation.git",
+ "reference": "03436ededa67c6e83b9b12defac15384cb399dc9"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/willdurand/Negotiation/zipball/03436ededa67c6e83b9b12defac15384cb399dc9",
+ "reference": "03436ededa67c6e83b9b12defac15384cb399dc9",
+ "shasum": ""
+ },
+ "require": {
+ "php": ">=5.4.0"
+ },
+ "require-dev": {
+ "phpunit/phpunit": "~4.5"
+ },
+ "type": "library",
+ "extra": {
+ "branch-alias": {
+ "dev-master": "2.3-dev"
+ }
+ },
+ "autoload": {
+ "psr-4": {
+ "Negotiation\\": "src/Negotiation"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "William Durand",
+ "email": "will+git@drnd.me"
+ }
+ ],
+ "description": "Content Negotiation tools for PHP provided as a standalone library.",
+ "homepage": "http://williamdurand.fr/Negotiation/",
+ "keywords": [
+ "accept",
+ "content",
+ "format",
+ "header",
+ "negotiation"
+ ],
+ "time": "2017-05-14T17:21:12+00:00"
+ },
{
"name": "zendframework/zend-code",
"version": "3.1.0",
diff --git a/config/bundles.php b/config/bundles.php
index c3f2ec2d..935def87 100644
--- a/config/bundles.php
+++ b/config/bundles.php
@@ -14,4 +14,5 @@ return [
Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true],
Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
+ FOS\RestBundle\FOSRestBundle::class => ['all' => true],
];
diff --git a/config/packages/fos_rest.yaml b/config/packages/fos_rest.yaml
new file mode 100644
index 00000000..0440800d
--- /dev/null
+++ b/config/packages/fos_rest.yaml
@@ -0,0 +1,15 @@
+# Read the documentation: https://symfony.com/doc/master/bundles/FOSRestBundle/index.html
+fos_rest: ~
+# param_fetcher_listener: true
+# allowed_methods_listener: true
+# routing_loader: true
+# view:
+# view_response_listener: true
+# exception:
+# codes:
+# App\Exception\MyException: 403
+# messages:
+# App\Exception\MyException: Forbidden area.
+# format_listener:
+# rules:
+# - { path: ^/api, prefer_extension: true, fallback_format: json, priorities: [ json, html ] }
diff --git a/src/Controller/ProductController.php b/src/Controller/ProductController.php
index 8964a541..bcd2e2eb 100644
--- a/src/Controller/ProductController.php
+++ b/src/Controller/ProductController.php
@@ -4,32 +4,33 @@ namespace App\Controller;
use App\Entity\Product;
use App\Entity\ProductCategory;
+use FOS\RestBundle\Controller\FOSRestController;
+use Symfony\Component\HttpFoundation\JsonResponse;
+use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Symfony\Component\Serializer\Encoder\JsonEncoder;
+use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
+use Symfony\Component\Serializer\Serializer;
-class ProductController extends Controller {
+class ProductController extends FOSRestController {
/**
* @Route("/", name="product")
*/
public function index() {
+ $normalizer = new ObjectNormalizer();
+ $normalizer->setCircularReferenceLimit( 1 );
+
+ $normalizer->setCircularReferenceHandler( function ( $object ) {
+ return $object->getName();
+ } );
+ $serializer = new Serializer( [ $normalizer ], [ new JsonEncoder() ] );
- $allProducts = $this->getDoctrine()
- ->getRepository( Product::class )
- ->findAll();
$allCategories = $this->getDoctrine()
->getRepository( ProductCategory::class )
->findAll();
+ $allCategories = $serializer->serialize( $allCategories, 'json' );
- // you can fetch the EntityManager via $this->getDoctrine()
- // or you can add an argument to your action: index(EntityManagerInterface $entityManager)
-// $entityManager = $this->getDoctrine()->getManager();
-
-
-// return new Response( 'Saved new product with id ' . $product->getId() );
- return $this->render( 'product/index.html.twig',
- [
- 'categories' => $allCategories,
- 'allProducts' => $allProducts,
- ] );
+ return new Response( $allCategories );
}
}
diff --git a/src/Entity/ProductCategory.php b/src/Entity/ProductCategory.php
index 95312bcd..963badb8 100644
--- a/src/Entity/ProductCategory.php
+++ b/src/Entity/ProductCategory.php
@@ -3,7 +3,6 @@
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
-
/**
* @ORM\Entity(repositoryClass="App\Repository\ProductCategoryRepository")
*/
diff --git a/symfony.lock b/symfony.lock
index a0b8a8ed..e0b12ac7 100644
--- a/symfony.lock
+++ b/symfony.lock
@@ -86,6 +86,15 @@
"fig/link-util": {
"version": "1.0.0"
},
+ "friendsofsymfony/rest-bundle": {
+ "version": "2.2",
+ "recipe": {
+ "repo": "github.com/symfony/recipes-contrib",
+ "branch": "master",
+ "version": "2.2",
+ "ref": "258300d52be6ad59b32a888d5ddafbf9638540ff"
+ }
+ },
"jdorn/sql-formatter": {
"version": "v1.2.17"
},
@@ -338,6 +347,9 @@
"ref": "3db029c03e452b4a23f7fc45cec7c922c2247eb8"
}
},
+ "symfony/templating": {
+ "version": "v3.4.6"
+ },
"symfony/translation": {
"version": "3.3",
"recipe": {
@@ -395,6 +407,12 @@
"webmozart/assert": {
"version": "1.3.0"
},
+ "willdurand/jsonp-callback-validator": {
+ "version": "v1.1.0"
+ },
+ "willdurand/negotiation": {
+ "version": "v2.3.1"
+ },
"zendframework/zend-code": {
"version": "3.1.0"
},
diff --git a/templates/product/index.html.twig b/templates/product/index.html.twig
index 62120260..e26dba07 100644
--- a/templates/product/index.html.twig
+++ b/templates/product/index.html.twig
@@ -19,13 +19,13 @@
{% for p in c.products %}
-
{% endfor %}
diff --git a/templates/product/test.html.twig b/templates/product/test.html.twig
new file mode 100644
index 00000000..1ea94fc8
--- /dev/null
+++ b/templates/product/test.html.twig
@@ -0,0 +1 @@
+essai qui marche