-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathcontroller.html.twig
132 lines (92 loc) · 3.34 KB
/
controller.html.twig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<p>Here are some useful shortcuts and functions regarding the request in Symfony 2 controllers.</p>
<h4>REQUEST and RESPONSE objects</h4>
<div class="row">
<div class="span6">
{% raw %}
<pre><code>$request->query->get('foo'); //gets foo GET var.
$request->request->get('bar'); //gets POST var.
$request->getMethod();
$request->server->get('HTTP_HOST'); //server variables.
$request->getPathInfo(); //gets the URI.
$request->files->get('file'); //files posted in a form.
$request->headers->get('content-type');
$request->cookies->get('PHPSESSID'); //cookies
$request->getLanguages();
$request->getPreferedLanguage(array('es','fr'));
$request->isXmlHttpRequest();
</code></pre>{% endraw %}
</div>
<div class="span6">
<p>Redirecting in a controller:</p>
{% raw %}
<pre><code>$this->redirect($this->generateUrl("homepage"));
// 2.6 and above
$this->redirectToRoute('homepage');
</code></pre>
{% endraw %}
<p>Rendering text from a controller:</p>
{% raw %}
<pre><code>return new Response('<html>…</html>');
</code></pre>{% endraw %}
<p>Forwarding:</p>
{% raw %}
<pre><code>return $this->forward('Bundle:Controller:Action');
</code></pre>{% endraw %}
<p>Redirect to 404 not found:</p>
{% raw %}
<pre><code>throw $this->createNotFoundException(message);
</code></pre>{% endraw %}
</div>
</div>
<div class="row">
<div class="span6">
<h4>Working with the session</h4>
<p>You can manage session attributes with:</p>
{% raw %}
<pre><code>$session = $this->getRequest()->getSession();
</code></pre>{% endraw %}
<p>or the shortcut version</p>
{% raw %}
<pre><code>$this->get('session');
</code></pre>{% endraw %}
<p>and to work with the data:</p>
{% raw %}
<pre><code>$session->get('foo','default value');
$session->set('foo','bar');
</code></pre>{% endraw %}
</div>
<div class="span6">
<h4>Flash messages</h4>
<p>Flash messages only last one request and they are stored in a FlashBag:</p>
{% raw %}
<pre><code>$this->addFlash('notice','message');
</code></pre>{% endraw %}
<p>To iterate trough all flash messages in a template you can use:</p>
{% raw %}
<pre><code>{% for flashMessage in app.session.flashbag.get('notice') %}
<div class="flash notice">
{{ flashMessage }}
</div>
{% endfor %}
</code></pre>{% endraw %}
</div>
</div>
<p>Finally, here is an example of a controller class with Request and Response object in use.</p>
{% raw %}<pre><code>namespace Symfony\CheatSheetBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends Controller
{
public function indexAction()
{
return $this->render('SymfonyCheatSheetBundle:Default:index.html.twig');
}
public function contactAction(Request $request)
{
//get request variables.
//do something, call service, go to database, create form, send emails, etc...
return $this->render('SymfonyCheatSheetBundle:Default:feedback.html.twig', array([template vars]));
}
}
</code></pre>{% endraw %}