|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony package. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.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 Symfony\Bundle\FrameworkBundle\Controller; |
|
13 |
|
14 use Symfony\Component\HttpFoundation\Response; |
|
15 use Symfony\Component\HttpFoundation\RedirectResponse; |
|
16 use Symfony\Component\DependencyInjection\ContainerAware; |
|
17 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
18 use Symfony\Component\Form\FormTypeInterface; |
|
19 use Symfony\Component\Form\Form; |
|
20 use Symfony\Component\Form\FormBuilder; |
|
21 use Symfony\Bundle\DoctrineBundle\Registry; |
|
22 use Symfony\Component\HttpFoundation\Request; |
|
23 |
|
24 /** |
|
25 * Controller is a simple implementation of a Controller. |
|
26 * |
|
27 * It provides methods to common features needed in controllers. |
|
28 * |
|
29 * @author Fabien Potencier <fabien@symfony.com> |
|
30 */ |
|
31 class Controller extends ContainerAware |
|
32 { |
|
33 /** |
|
34 * Generates a URL from the given parameters. |
|
35 * |
|
36 * @param string $name The name of the route |
|
37 * @param mixed $parameters An array of parameters |
|
38 * @param Boolean $absolute Whether to generate an absolute URL |
|
39 * |
|
40 * @return string The generated URL |
|
41 */ |
|
42 public function generateUrl($route, $parameters = array(), $absolute = false) |
|
43 { |
|
44 return $this->container->get('router')->generate($route, $parameters, $absolute); |
|
45 } |
|
46 |
|
47 /** |
|
48 * Forwards the request to another controller. |
|
49 * |
|
50 * @param string $controller The controller name (a string like BlogBundle:Post:index) |
|
51 * @param array $path An array of path parameters |
|
52 * @param array $query An array of query parameters |
|
53 * |
|
54 * @return Response A Response instance |
|
55 */ |
|
56 public function forward($controller, array $path = array(), array $query = array()) |
|
57 { |
|
58 return $this->container->get('http_kernel')->forward($controller, $path, $query); |
|
59 } |
|
60 |
|
61 /** |
|
62 * Returns a RedirectResponse to the given URL. |
|
63 * |
|
64 * @param string $url The URL to redirect to |
|
65 * @param integer $status The status code to use for the Response |
|
66 * |
|
67 * @return RedirectResponse |
|
68 */ |
|
69 public function redirect($url, $status = 302) |
|
70 { |
|
71 return new RedirectResponse($url, $status); |
|
72 } |
|
73 |
|
74 /** |
|
75 * Returns a rendered view. |
|
76 * |
|
77 * @param string $view The view name |
|
78 * @param array $parameters An array of parameters to pass to the view |
|
79 * |
|
80 * @return string The renderer view |
|
81 */ |
|
82 public function renderView($view, array $parameters = array()) |
|
83 { |
|
84 return $this->container->get('templating')->render($view, $parameters); |
|
85 } |
|
86 |
|
87 /** |
|
88 * Renders a view. |
|
89 * |
|
90 * @param string $view The view name |
|
91 * @param array $parameters An array of parameters to pass to the view |
|
92 * @param Response $response A response instance |
|
93 * |
|
94 * @return Response A Response instance |
|
95 */ |
|
96 public function render($view, array $parameters = array(), Response $response = null) |
|
97 { |
|
98 return $this->container->get('templating')->renderResponse($view, $parameters, $response); |
|
99 } |
|
100 |
|
101 /** |
|
102 * Returns a NotFoundHttpException. |
|
103 * |
|
104 * This will result in a 404 response code. Usage example: |
|
105 * |
|
106 * throw $this->createNotFoundException('Page not found!'); |
|
107 * |
|
108 * @return NotFoundHttpException |
|
109 */ |
|
110 public function createNotFoundException($message = 'Not Found', \Exception $previous = null) |
|
111 { |
|
112 return new NotFoundHttpException($message, $previous); |
|
113 } |
|
114 |
|
115 /** |
|
116 * Creates and returns a Form instance from the type of the form. |
|
117 * |
|
118 * @param string|FormTypeInterface $type The built type of the form |
|
119 * @param mixed $data The initial data for the form |
|
120 * @param array $options Options for the form |
|
121 * |
|
122 * @return Form |
|
123 */ |
|
124 public function createForm($type, $data = null, array $options = array()) |
|
125 { |
|
126 return $this->container->get('form.factory')->create($type, $data, $options); |
|
127 } |
|
128 |
|
129 /** |
|
130 * Creates and returns a form builder instance |
|
131 * |
|
132 * @param mixed $data The initial data for the form |
|
133 * @param array $options Options for the form |
|
134 * |
|
135 * @return FormBuilder |
|
136 */ |
|
137 public function createFormBuilder($data = null, array $options = array()) |
|
138 { |
|
139 return $this->container->get('form.factory')->createBuilder('form', $data, $options); |
|
140 } |
|
141 |
|
142 /** |
|
143 * Shortcut to return the request service. |
|
144 * |
|
145 * @return Request |
|
146 */ |
|
147 public function getRequest() |
|
148 { |
|
149 return $this->container->get('request'); |
|
150 } |
|
151 |
|
152 /** |
|
153 * Shortcut to return the Doctrine Registry service. |
|
154 * |
|
155 * @return Registry |
|
156 * |
|
157 * @throws \LogicException If DoctrineBundle is not available |
|
158 */ |
|
159 public function getDoctrine() |
|
160 { |
|
161 if (!$this->container->has('doctrine')) { |
|
162 throw new \LogicException('The DoctrineBundle is not installed in your application.'); |
|
163 } |
|
164 |
|
165 return $this->container->get('doctrine'); |
|
166 } |
|
167 |
|
168 /** |
|
169 * Returns true if the service id is defined. |
|
170 * |
|
171 * @param string $id The service id |
|
172 * |
|
173 * @return Boolean true if the service id is defined, false otherwise |
|
174 */ |
|
175 public function has($id) |
|
176 { |
|
177 return $this->container->has($id); |
|
178 } |
|
179 |
|
180 /** |
|
181 * Gets a service by id. |
|
182 * |
|
183 * @param string $id The service id |
|
184 * |
|
185 * @return object The service |
|
186 */ |
|
187 public function get($id) |
|
188 { |
|
189 return $this->container->get($id); |
|
190 } |
|
191 } |