Skip to content

Commit 2240da7

Browse files
committed
Merge pull request #574 from merk/view-redirections
Change how redirections are created
2 parents c9b2751 + cccefd9 commit 2240da7

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

UPGRADING.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ Upgrading
44
Note as FOSRestBundle is not yet declared stable, this document will be updated to
55
list important BC breaks.
66

7+
### upgrading to 2.0.0 (unreleased)
8+
9+
* RedirectView and RouteRedirect view were removed. Use View::createRedirect and
10+
View::createRouteRedirect instead. Note: the default status code for a route redirect
11+
has changed from HTTP_CREATED (201) to HTTP_FOUND (302).
12+
713
### upgrading from 0.13.1
814

915
* ExceptionController::showAction() doesn't have type hint on the $exception object anymore due to a BC change

View/RedirectView.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ class RedirectView
2727
* @param string $url
2828
* @param integer $statusCode
2929
* @param array $headers
30+
*
31+
* @deprecated To be removed in FOSRestBundle 2.0.0. Use View::createRedirect instead.
3032
*/
3133
public static function create($url, $statusCode = Codes::HTTP_FOUND, array $headers = array())
3234
{
33-
return View::create(null, $statusCode, $headers)->setLocation($url);
35+
return View::createRedirect($url, $statusCode, $headers);
3436
}
3537
}

View/RouteRedirectView.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ class RouteRedirectView
2828
* @param mixed $parameters
2929
* @param integer $statusCode
3030
* @param array $headers
31+
*
32+
* @deprecated To be removed in FOSRestBundle 2.0.0. Use View::createRouteRedirect instead.
3133
*/
3234
public static function create($route, array $parameters = array(), $statusCode = Codes::HTTP_CREATED, array $headers = array())
3335
{
34-
return View::create(null, $statusCode, $headers)->setRoute($route)->setRouteParameters($parameters);
36+
return View::createRouteRedirect($route, $parameters, $statusCode, $headers);
3537
}
3638
}

View/View.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace FOS\RestBundle\View;
1313

14+
use FOS\Rest\Util\Codes;
1415
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
1516
use Symfony\Component\HttpFoundation\Response;
1617

@@ -87,6 +88,46 @@ public static function create($data = null, $statusCode = null, array $headers =
8788
return new static($data, $statusCode, $headers);
8889
}
8990

91+
/**
92+
* Convenience method to allow for a fluent interface while creating a redirect to a
93+
* given url.
94+
*
95+
* @param string $url
96+
* @param int $statusCode
97+
* @param array $headers
98+
* @return View
99+
*/
100+
public static function createRedirect($url, $statusCode = Codes::HTTP_FOUND, array $headers = array())
101+
{
102+
$view = static::create(null, $statusCode, $headers);
103+
$view->setLocation($url);
104+
105+
return $view;
106+
}
107+
108+
/**
109+
* Convenience method to allow for a fluent interface while creating a redirect to a
110+
* given route.
111+
*
112+
* @param string $route
113+
* @param array $parameters
114+
* @param int $statusCode
115+
* @param array $headers
116+
* @return View
117+
*/
118+
public static function createRouteRedirect(
119+
$route,
120+
array $parameters = array(),
121+
$statusCode = Codes::HTTP_FOUND,
122+
array $headers = array()
123+
) {
124+
$view = static::create(null, $statusCode, $headers);
125+
$view->setRoute($route);
126+
$view->setRouteParameters($parameters);
127+
128+
return $view;
129+
}
130+
90131
/**
91132
* Constructor
92133
*

0 commit comments

Comments
 (0)