Skip to content

Commit 1ffcab0

Browse files
committed
Add examples to UPGRADE-2.0.md
1 parent 6cb0673 commit 1ffcab0

File tree

1 file changed

+145
-2
lines changed

1 file changed

+145
-2
lines changed

UPGRADING-2.0.md

Lines changed: 145 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Upgrading From 1.x To 2.0
2424
2525
* dropped support for the legacy ``Symfony\Component\Validator\ValidatorInterface``
2626
27-
* removed ``FOS\RestBundle\Util\Codes`` in favor of ``Symfony\Component\HttpFoundation\Response``
27+
* removed ``FOS\RestBundle\Util\Codes`` in favor of ``Symfony\Component\HttpFoundation\Response`` constants
2828
2929
* compatibility with Symfony <2.7, JMS Serializer/SerializerBundle <1.0 and SensioFrameworkExtraBundle <3.0 was dropped
3030
@@ -38,12 +38,93 @@ Upgrading From 1.x To 2.0
3838
3939
* removed ``callback_filter`` configuration option for the jsonp_handler
4040
41-
* ``exception_wrapper_handler`` is now the name of a service and not the name of a class
41+
* the ``exception_wrapper_handler`` config option was removed. Use normalizers instead.
42+
43+
Before:
44+
```yml
45+
# config.yml
46+
47+
fos_rest:
48+
view:
49+
exception_wrapper_handler: AppBundle\ExceptionWrapperHandler
50+
```
51+
```php
52+
namespace AppBundle;
53+
54+
class ExceptionWrapperHandler implements ExceptionWrapperHandlerInterface
55+
{
56+
public function wrap($data)
57+
{
58+
return new ExceptionWrapper(array('status_code' => 'foo'));
59+
}
60+
}
61+
```
62+
63+
After (if you use the Symfony serializer):
64+
```yml
65+
# services.yml
66+
67+
services:
68+
app_bundle.exception_normalizer:
69+
class: AppBundle\Normalizer\ExceptionNormalizer
70+
tags:
71+
- { name: serializer.normalizer }
72+
```
73+
```php
74+
namespace AppBundle\Normalizer;
75+
76+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
77+
78+
class ExceptionNormalizer implements NormalizerInterface
79+
{
80+
public function normalize($object, $format = null, array $context = array())
81+
{
82+
return array('status_code' => 'foo');
83+
}
84+
85+
public function supportsNormalization($data, $format = null)
86+
{
87+
return $data instanceof \My\Exception;
88+
}
89+
}
90+
```
4291

4392
* removed all ``.class`` parameters, instead overwriting services via explicit Bundle configuration is preferred
4493

4594
* renamed ``AbstractScalarParam::$array`` to ``AbstractScalarParam::$map``
4695

96+
Before:
97+
```php
98+
namespace AppBundle\Controller;
99+
100+
class MyController
101+
{
102+
/**
103+
* @RequestParam(name="foo", array=true)
104+
*/
105+
public function myAction()
106+
{
107+
// ...
108+
}
109+
}
110+
```
111+
112+
After:
113+
```php
114+
namespace AppBundle\Controller;
115+
116+
class MyController
117+
{
118+
/**
119+
* @RequestParam(name="foo", map=true)
120+
*/
121+
public function myAction()
122+
{
123+
// ...
124+
}
125+
}
126+
```
127+
47128
* added `ControllerTrait` for developers that prefer to use DI for their controllers instead of extending ``FOSRestController``
48129

49130
* when having an action called ``lockUserAction``, then it will have to use the http method ``LOCK`` (RFC-2518) instead of ``PATCH``. The following methods are affected by this change
@@ -57,10 +138,72 @@ Upgrading From 1.x To 2.0
57138

58139
* removed the ability of the ``AccessDeniedListener`` to render a response. Use the FOSRestBundle or the twig exception controller in complement.
59140

141+
Before:
142+
```yml
143+
# config.yml
144+
145+
fos_rest:
146+
access_denied_listener: true
147+
```
148+
149+
After:
150+
```yml
151+
# config.yml
152+
153+
fos_rest:
154+
access_denied_listener: true
155+
exception: true # Activates the FOSRestBundle exception controller
156+
```
157+
60158
* changed the priority of ``RequestBodyParamConverter`` to ``-50``
61159
62160
* made silent the ``RequestBodyParamConverter`` when a parameter is optional and it can't resolve it
63161
64162
* removed the ``format_negotiator`` option ``exception_fallback_format``; you can match the ``ExceptionController`` thanks to the ``attributes`` option instead
65163
164+
Before:
165+
```yml
166+
# config.yml
167+
168+
fos_rest:
169+
format_listener:
170+
rules:
171+
- { path: ^/, fallback_format: html, exception_fallback_format: json }
172+
```
173+
174+
After:
175+
```yml
176+
# config.yml
177+
178+
fos_rest:
179+
format_listener:
180+
rules:
181+
- { path: ^/, fallback_format: json, attributes: { _controller: FOS\RestBundle\Controller\ExceptionController } }
182+
- { path: ^/, fallback_format: html } }
183+
```
184+
66185
* `View::setSerializationContext` and `View::getSerializationContext` have been removed. Use `View::setContext` and `View::getContext` together with the new Context class instead.
186+
187+
Before:
188+
```php
189+
use JMS\Serializer\SerializationContext;
190+
191+
$view = new View();
192+
193+
$context = new SerializationContext();
194+
$view->setSerializationContext($context);
195+
196+
$context = $view->getSerializationContext();
197+
```
198+
199+
After:
200+
```php
201+
use FOS\RestBundle\Context\Context;
202+
203+
$view = new View();
204+
205+
$context = new Context();
206+
$view->setContext($context);
207+
208+
$context = $view->getContext();
209+
```

0 commit comments

Comments
 (0)