|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FOSRestBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace FOS\RestBundle\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use FOS\RestBundle\View\View; |
|
15
|
|
|
use FOS\RestBundle\View\ViewHandlerInterface; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Trait for Controllers using the View functionality of FOSRestBundle. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Benjamin Eberlei <[email protected]> |
|
22
|
|
|
* @author Lukas Kahwe Smith <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
trait ControllerTrait |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var ViewHandlerInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $viewhandler; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Get the ViewHandler. |
|
33
|
|
|
* |
|
34
|
|
|
* @param ViewHandlerInterface $viewhandler |
|
35
|
|
|
*/ |
|
36
|
|
|
public function setViewHandler(ViewHandlerInterface $viewhandler) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->viewhandler = $viewhandler; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get the ViewHandler. |
|
43
|
|
|
* |
|
44
|
|
|
* @return ViewHandlerInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function getViewHandler() |
|
47
|
|
|
{ |
|
48
|
|
|
if (!$this->viewhandler instanceof ViewHandlerInterface) { |
|
49
|
|
|
throw new \RuntimeException('A "ViewHandlerInterface" instance must be set when usign the FOSRestBundle "ControllerTrait".'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $this->viewhandler; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Creates a view. |
|
57
|
|
|
* |
|
58
|
|
|
* Convenience method to allow for a fluent interface. |
|
59
|
|
|
* |
|
60
|
|
|
* @param mixed $data |
|
61
|
|
|
* @param int $statusCode |
|
62
|
|
|
* @param array $headers |
|
63
|
|
|
* |
|
64
|
|
|
* @return View |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function view($data = null, $statusCode = null, array $headers = []) |
|
67
|
|
|
{ |
|
68
|
|
|
return View::create($data, $statusCode, $headers); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Creates a Redirect view. |
|
73
|
|
|
* |
|
74
|
|
|
* Convenience method to allow for a fluent interface. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $url |
|
77
|
|
|
* @param int $statusCode |
|
78
|
|
|
* @param array $headers |
|
79
|
|
|
* |
|
80
|
|
|
* @return View |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function redirectView($url, $statusCode = Response::HTTP_FOUND, array $headers = []) |
|
83
|
|
|
{ |
|
84
|
|
|
return View::createRedirect($url, $statusCode, $headers); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Creates a Route Redirect View. |
|
89
|
|
|
* |
|
90
|
|
|
* Convenience method to allow for a fluent interface. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $route |
|
93
|
|
|
* @param mixed $parameters |
|
94
|
|
|
* @param int $statusCode |
|
95
|
|
|
* @param array $headers |
|
96
|
|
|
* |
|
97
|
|
|
* @return View |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function routeRedirectView($route, array $parameters = [], $statusCode = Response::HTTP_CREATED, array $headers = []) |
|
100
|
|
|
{ |
|
101
|
|
|
return View::createRouteRedirect($route, $parameters, $statusCode, $headers); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Converts view into a response object. |
|
106
|
|
|
* |
|
107
|
|
|
* Not necessary to use, if you are using the "ViewResponseListener", which |
|
108
|
|
|
* does this conversion automatically in kernel event "onKernelView". |
|
109
|
|
|
* |
|
110
|
|
|
* @param View $view |
|
111
|
|
|
* |
|
112
|
|
|
* @return Response |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function handleView(View $view) |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->getViewHandler()->handle($view); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|