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
|
|
|
private $viewhandler; |
27
|
|
|
|
28
|
|
|
public function setViewHandler(ViewHandlerInterface $viewhandler) |
29
|
|
|
{ |
30
|
|
|
$this->viewhandler = $viewhandler; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function getViewHandler() |
34
|
|
|
{ |
35
|
|
|
if (!$this->viewhandler instanceof ViewHandlerInterface) { |
36
|
|
|
throw new \RuntimeException('A "ViewHandlerInterface" instance must be set when using the FOSRestBundle "ControllerTrait".'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $this->viewhandler; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return View |
44
|
|
|
*/ |
45
|
6 |
|
protected function view($data = null, ?int $statusCode = null, array $headers = []) |
46
|
|
|
{ |
47
|
6 |
|
return View::create($data, $statusCode, $headers); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return View |
52
|
|
|
*/ |
53
|
|
|
protected function redirectView(string $url, int $statusCode = Response::HTTP_FOUND, array $headers = []) |
54
|
|
|
{ |
55
|
|
|
return View::createRedirect($url, $statusCode, $headers); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return View |
60
|
|
|
*/ |
61
|
1 |
|
protected function routeRedirectView(string $route, array $parameters = [], int $statusCode = Response::HTTP_CREATED, array $headers = []) |
62
|
|
|
{ |
63
|
1 |
|
return View::createRouteRedirect($route, $parameters, $statusCode, $headers); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Converts view into a response object. |
68
|
|
|
* |
69
|
|
|
* Not necessary to use, if you are using the "ViewResponseListener", which |
70
|
|
|
* does this conversion automatically in kernel event "onKernelView". |
71
|
|
|
* |
72
|
|
|
* @return Response |
73
|
|
|
*/ |
74
|
|
|
protected function handleView(View $view) |
75
|
|
|
{ |
76
|
|
|
return $this->getViewHandler()->handle($view); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|