Skip to content

Commit 44f2ff8

Browse files
committed
more work on getting started docs
1 parent 218e2d4 commit 44f2ff8

File tree

2 files changed

+26
-45
lines changed

2 files changed

+26
-45
lines changed

controller.rst

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,12 @@
44
Controller
55
==========
66

7-
A controller is a PHP function you create that reads information from the Symfony's
7+
A controller is a PHP function you create that reads information from the
88
``Request`` object and creates and returns a ``Response`` object. The response could
99
be an HTML page, JSON, XML, a file download, a redirect, a 404 error or anything
1010
else you can dream up. The controller executes whatever arbitrary logic
1111
*your application* needs to render the content of a page.
1212

13-
See how simple this is by looking at a Symfony controller in action.
14-
This renders a page that prints a lucky (random) number::
15-
16-
// src/Controller/LuckyController.php
17-
namespace App\Controller;
18-
19-
use Symfony\Component\HttpFoundation\Response;
20-
use Symfony\Component\Routing\Annotation\Route;
21-
22-
class LuckyController
23-
{
24-
/**
25-
* @Route("/lucky/number")
26-
*/
27-
public function numberAction()
28-
{
29-
$number = mt_rand(0, 100);
30-
31-
return new Response(
32-
'<html><body>Lucky number: '.$number.'</body></html>'
33-
);
34-
}
35-
}
36-
37-
But in the real world, your controller will probably do a lot of work in order to
38-
create the response. It might read information from the request, load data from a
39-
database (or API), send an email or set information on the user's session.
40-
But in all cases, the controller will eventually return the ``Response`` object
41-
that will be delivered back to the client.
42-
4313
.. tip::
4414

4515
If you haven't already created your first working page, check out
@@ -64,7 +34,7 @@ class::
6434
class LuckyController
6535
{
6636
/**
67-
* @Route("/lucky/number/{max}")
37+
* @Route("/lucky/number/{max}", name="app_lucky_number")
6838
*/
6939
public function number($max)
7040
{
@@ -126,14 +96,17 @@ To make life nicer, Symfony comes with two optional base
12696
You can extend either to get access to some `helper methods`_.
12797

12898
Add the ``use`` statement atop your controller class and then modify
129-
``LuckyController`` to extend it::
99+
``LuckyController`` to extend it:
100+
101+
.. code-block:: diff
130102
131103
// src/Controller/LuckyController.php
132104
namespace App\Controller;
133105
134-
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
106+
+ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
135107
136-
class LuckyController extends Controller
108+
- class LuckyController
109+
+ class LuckyController extends Controller
137110
{
138111
// ...
139112
}
@@ -161,7 +134,7 @@ Generating URLs
161134
The :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::generateUrl`
162135
method is just a helper method that generates the URL for a given route::
163136

164-
$url = $this->generateUrl('blog_show', array('slug' => 'slug-value'));
137+
$url = $this->generateUrl('app_lucky_number', array('max' => 10));
165138

166139
Redirecting
167140
~~~~~~~~~~~
@@ -184,18 +157,16 @@ and ``redirect()`` methods::
184157
return $this->redirectToRoute('homepage', array(), 301);
185158

186159
// redirect to a route with parameters
187-
return $this->redirectToRoute('blog_show', array('slug' => 'my-page'));
160+
return $this->redirectToRoute('app_lucky_number', array('max' => 10));
188161

189162
// redirect externally
190163
return $this->redirect('http://symfony.com/doc');
191164
}
192165

193-
For more information, see the :doc:`Routing article </routing>`.
194-
195166
.. caution::
196167

197168
The ``redirect()`` method does not check its destination in any way. If you
198-
redirect to some URL provided byend-users, your application may be open
169+
redirect to a URL provided by end-users, your application may be open
199170
to the `unvalidated redirects security vulnerability`_.
200171

201172
.. index::

page_creation.rst

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ To get a list of *all* of the routes in your system, use the ``debug:router`` co
166166
167167
$ php bin/console debug:router
168168
169+
You should see your *one* route so far:
170+
171+
------------------ -------- -------- ------ ---------------
172+
Name Method Scheme Host Path
173+
------------------ -------- -------- ------ ---------------
174+
app_lucky_number ANY ANY ANY /lucky/number
175+
------------------ -------- -------- ------ ---------------
176+
169177
You'll learn about many more commands as you continue!
170178

171179
The Web Debug Toolbar: Debugging Dream
@@ -203,15 +211,17 @@ First, install Twig:
203211
$ composer require twig
204212
205213
Second, make sure that ``LuckyController`` extends Symfony's base
206-
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller` class::
214+
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller` class:
215+
216+
.. code-block:: diff
207217
208218
// src/Controller/LuckyController.php
209219
210220
// ...
211-
// --> add this new use statement
212-
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
221+
+ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
213222
214-
class LuckyController extends Controller
223+
- class LuckyController
224+
+ class LuckyController extends Controller
215225
{
216226
// ...
217227
}
@@ -268,7 +278,7 @@ project:
268278
``src/``
269279
All your PHP code lives here.
270280

271-
99% of the time, you'll be working in ``src/`` (PHP files) or ``config/`` (everything
281+
Most of the time, you'll be working in ``src/`` (PHP files) or ``config/`` (everything
272282
else). As you keep reading, you'll learn what can be done inside each of these.
273283

274284
So what about the other directories in the project?

0 commit comments

Comments
 (0)