Skip to content

Commit b95af09

Browse files
committed
Updated controller/* articles to Symfony 4
1 parent 845f424 commit b95af09

File tree

7 files changed

+58
-69
lines changed

7 files changed

+58
-69
lines changed

controller/argument_value_resolver.rst

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ Symfony ships with five value resolvers in the HttpKernel component:
4242
argument list. When the action is called, the last (variadic) argument will
4343
contain all the values of this array.
4444

45-
.. note::
46-
47-
Prior to Symfony 3.1, this logic was resolved within the ``ControllerResolver``.
48-
The old functionality is rewritten to the aforementioned value resolvers.
49-
5045
Adding a Custom Value Resolver
5146
------------------------------
5247

@@ -62,7 +57,7 @@ controller::
6257

6358
class UserController
6459
{
65-
public function indexAction(User $user)
60+
public function index(User $user)
6661
{
6762
return new Response('Hello '.$user->getUsername().'!');
6863
}

controller/csrf_token_validation.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ want to use the Symfony Form component. If, for example, you are implementing
99
a DELETE action, you can use the :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::isCsrfTokenValid`
1010
method to check the validity of a CSRF token::
1111

12-
public function deleteAction()
12+
public function delete()
1313
{
1414
if ($this->isCsrfTokenValid('token_id', $submittedToken)) {
1515
// ... do something, like deleting an object

controller/error_pages.rst

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -67,33 +67,32 @@ logic to determine the template filename:
6767

6868
To override these templates, simply rely on the standard Symfony method for
6969
:doc:`overriding templates that live inside a bundle </templating/overriding>`:
70-
put them in the ``app/Resources/TwigBundle/views/Exception/`` directory.
70+
put them in the ``templates/bundles/TwigBundle/Exception/`` directory.
7171

7272
A typical project that returns HTML and JSON pages, might look like this:
7373

7474
.. code-block:: text
7575
76-
app/
77-
└─ Resources/
76+
templates/
77+
└─ bundles/
7878
└─ TwigBundle/
79-
└─ views/
80-
└─ Exception/
81-
├─ error404.html.twig
82-
├─ error403.html.twig
83-
├─ error.html.twig # All other HTML errors (including 500)
84-
├─ error404.json.twig
85-
├─ error403.json.twig
86-
└─ error.json.twig # All other JSON errors (including 500)
79+
└─ Exception/
80+
├─ error404.html.twig
81+
├─ error403.html.twig
82+
├─ error.html.twig # All other HTML errors (including 500)
83+
├─ error404.json.twig
84+
├─ error403.json.twig
85+
└─ error.json.twig # All other JSON errors (including 500)
8786
8887
Example 404 Error Template
8988
--------------------------
9089

9190
To override the 404 error template for HTML pages, create a new
92-
``error404.html.twig`` template located at ``app/Resources/TwigBundle/views/Exception/``:
91+
``error404.html.twig`` template located at ``templates/bundles/TwigBundle/Exception/``:
9392

9493
.. code-block:: html+twig
9594

96-
{# app/Resources/TwigBundle/views/Exception/error404.html.twig #}
95+
{# templates/bundles/TwigBundle/Exception/error404.html.twig #}
9796
{% extends 'base.html.twig' %}
9897

9998
{% block body %}
@@ -139,21 +138,22 @@ what it looks like and debug it?
139138
Fortunately, the default ``ExceptionController`` allows you to preview your
140139
*error* pages during development.
141140

142-
To use this feature, you need to have a definition in your
143-
``routing_dev.yml`` file like so:
141+
To use this feature, you need to load some special routes provided by TwigBundle
142+
(if the application uses :doc:`Symfony Flex </setup/flex>` they are loaded
143+
automatically when installing Twig support):
144144

145145
.. configuration-block::
146146

147147
.. code-block:: yaml
148148
149-
# app/config/routing_dev.yml
149+
# config/routes/dev/twig.yaml
150150
_errors:
151-
resource: "@TwigBundle/Resources/config/routing/errors.xml"
151+
resource: '@TwigBundle/Resources/config/routing/errors.xml'
152152
prefix: /_error
153153
154154
.. code-block:: xml
155155
156-
<!-- app/config/routing_dev.xml -->
156+
<!-- config/routes/dev/twig.xml -->
157157
<?xml version="1.0" encoding="UTF-8" ?>
158158
<routes xmlns="http://symfony.com/schema/routing"
159159
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -166,7 +166,7 @@ To use this feature, you need to have a definition in your
166166
167167
.. code-block:: php
168168
169-
// app/config/routing_dev.php
169+
// config/routes/dev/twig.php
170170
use Symfony\Component\Routing\RouteCollection;
171171
172172
$collection = new RouteCollection();
@@ -177,20 +177,14 @@ To use this feature, you need to have a definition in your
177177
178178
return $collection;
179179
180-
If you're coming from an older version of Symfony, you might need to
181-
add this to your ``routing_dev.yml`` file. If you're starting from
182-
scratch, the `Symfony Standard Edition`_ already contains it for you.
183-
184-
With this route added, you can use URLs like
180+
With this route added, you can use URLs like these to preview the *error* page
181+
for a given status code as HTML or for a given status code and format.
185182

186183
.. code-block:: text
187184
188185
http://localhost/index.php/_error/{statusCode}
189186
http://localhost/index.php/_error/{statusCode}.{format}
190187
191-
to preview the *error* page for a given status code as HTML or for a
192-
given status code and format.
193-
194188
.. _custom-exception-controller:
195189
.. _replacing-the-default-exceptioncontroller:
196190

@@ -209,13 +203,13 @@ configuration option to point to it:
209203

210204
.. code-block:: yaml
211205
212-
# app/config/config.yml
206+
# config/packages/twig.yaml
213207
twig:
214-
exception_controller: AppBundle:Exception:showException
208+
exception_controller: App\Controller\ExceptionController::showException
215209
216210
.. code-block:: xml
217211
218-
<!-- app/config/config.xml -->
212+
<!-- config/packages/twig.xml -->
219213
<?xml version="1.0" encoding="UTF-8" ?>
220214
<container xmlns="http://symfony.com/schema/dic/services"
221215
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -226,16 +220,16 @@ configuration option to point to it:
226220
http://symfony.com/schema/dic/twig/twig-1.0.xsd">
227221
228222
<twig:config>
229-
<twig:exception-controller>AppBundle:Exception:showException</twig:exception-controller>
223+
<twig:exception-controller>App\Controller\ExceptionController::showException</twig:exception-controller>
230224
</twig:config>
231225
232226
</container>
233227
234228
.. code-block:: php
235229
236-
// app/config/config.php
230+
// config/packages/twig.php
237231
$container->loadFromExtension('twig', array(
238-
'exception_controller' => 'AppBundle:Exception:showException',
232+
'exception_controller' => 'App\Controller\ExceptionController::showException',
239233
// ...
240234
));
241235

controller/forwarding.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ sub-request and calls the defined controller. The ``forward()`` method returns
1111
the :class:`Symfony\\Component\\HttpFoundation\\Response` object that is returned
1212
from *that* controller::
1313

14-
public function indexAction($name)
14+
public function index($name)
1515
{
16-
$response = $this->forward('AppBundle:Something:fancy', array(
16+
$response = $this->forward('App\Controller\OtherController::fancy', array(
1717
'name' => $name,
1818
'color' => 'green',
1919
));
@@ -26,10 +26,10 @@ from *that* controller::
2626
The array passed to the method becomes the arguments for the resulting controller.
2727
The target controller method might look something like this::
2828

29-
public function fancyAction($name, $color)
29+
public function fancy($name, $color)
3030
{
3131
// ... create and return a Response object
3232
}
3333

3434
Just like when creating a controller for a route, the order of the arguments
35-
of ``fancyAction()`` doesn't matter: the matching is done by name.
35+
of the ``fancy()`` method doesn't matter: the matching is done by name.

controller/service.rst

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ Referencing your Service from Routing
1515
Registering your controller as a service is great, but you also need to make sure
1616
that your routing references the service properly, so that Symfony knows to use it.
1717

18-
If the service id is the fully-qualified class name (FQCN) of your controller, you're
19-
done! You can use the normal ``AppBundle:Hello:index`` syntax in your routing and
20-
it will find your service.
18+
If the service id is the fully-qualified class name (FQCN) of your controller,
19+
you're done! You can use the normal ``App\Controller\HelloController::index``
20+
syntax in your routing and it will find your service.
2121

22-
But, if your service has a different id, you can use a special ``SERVICEID:METHOD``
23-
syntax:
22+
But, if your service has a different id, you can use a special
23+
``service_id:method_name`` syntax:
2424

2525
.. configuration-block::
2626

@@ -31,8 +31,6 @@ syntax:
3131
// You need to use Sensio's annotation to specify a service id
3232
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
3333
// ...
34-
35-
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
3634
3735
/**
3836
* @Route(service="app.hello_controller")
@@ -73,8 +71,8 @@ syntax:
7371
7472
.. note::
7573

76-
You cannot drop the ``Action`` part of the method name when using the
77-
single colon notation.
74+
When using the ``service_id:method_name`` syntax, the method name must
75+
end with the ``Action`` suffix.
7876

7977
.. _controller-service-invoke:
8078

@@ -114,7 +112,7 @@ service and use it directly::
114112
$this->twig = $twig;
115113
}
116114

117-
public function indexAction($name)
115+
public function index($name)
118116
{
119117
$content = $this->twig->render(
120118
'hello/index.html.twig',

controller/soap_web_service.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ the :ref:`default services configuration <service-container-services-load-exampl
5454
you don't need to do anything!
5555

5656
Finally, below is an example of a controller that is capable of handling a SOAP
57-
request. Because ``indexAction()`` is accessible via ``/soap``, the WSDL document
57+
request. Because ``index()`` is accessible via ``/soap``, the WSDL document
5858
can be retrieved via ``/soap?wsdl``::
5959

6060
namespace App\Controller;
@@ -69,7 +69,7 @@ can be retrieved via ``/soap?wsdl``::
6969
/**
7070
* @Route("/soap")
7171
*/
72-
public function indexAction(HelloService $helloService)
72+
public function index(HelloService $helloService)
7373
{
7474
$server = new \SoapServer('/path/to/hello.wsdl');
7575
$server->setObject($helloService);
@@ -96,8 +96,8 @@ into the content of the Response and clear the output buffer. Finally, you're
9696
ready to return the ``Response``.
9797

9898
Below is an example calling the service using a `NuSOAP`_ client. This example
99-
assumes that the ``indexAction()`` in the controller above is accessible via the
100-
route ``/soap``::
99+
assumes that the ``index()`` method in the controller above is accessible via
100+
the route ``/soap``::
101101

102102
$client = new \Soapclient('http://example.com/index.php/soap?wsdl');
103103

controller/upload_file.rst

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ to :doc:`customize form rendering </form/form_customization>`):
108108
Finally, you need to update the code of the controller that handles the form::
109109

110110
// src/Controller/ProductController.php
111-
namespace App\ProductController;
111+
namespace App\Controller;
112112

113113
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
114114
use Symfony\Component\HttpFoundation\Request;
@@ -121,7 +121,7 @@ Finally, you need to update the code of the controller that handles the form::
121121
/**
122122
* @Route("/product/new", name="app_product_new")
123123
*/
124-
public function newAction(Request $request)
124+
public function new(Request $request)
125125
{
126126
$product = new Product();
127127
$form = $this->createForm(ProductType::class, $product);
@@ -161,7 +161,7 @@ controller to specify the directory in which the brochures should be stored:
161161

162162
.. code-block:: yaml
163163
164-
# app/config/config.yml
164+
# config/services.yaml
165165
166166
# ...
167167
parameters:
@@ -294,7 +294,7 @@ Now you're ready to use this service in the controller::
294294
use App\Service\FileUploader;
295295

296296
// ...
297-
public function newAction(Request $request, FileUploader $fileUploader)
297+
public function new(Request $request, FileUploader $fileUploader)
298298
{
299299
// ...
300300

@@ -393,14 +393,16 @@ Now, register this class as a Doctrine listener:
393393
xsi:schemaLocation="http://symfony.com/schema/dic/services
394394
http://symfony.com/schema/dic/services/services-1.0.xsd">
395395
396-
<!-- ... be sure autowiring is enabled -->
397-
<defaults autowire="true" />
398-
<!-- ... -->
396+
<services>
397+
<!-- ... be sure autowiring is enabled -->
398+
<defaults autowire="true" />
399+
<!-- ... -->
399400
400-
<service id="App\EventListener\BrochureUploaderListener">
401-
<tag name="doctrine.event_listener" event="prePersist"/>
402-
<tag name="doctrine.event_listener" event="preUpdate"/>
403-
</service>
401+
<service id="App\EventListener\BrochureUploaderListener">
402+
<tag name="doctrine.event_listener" event="prePersist"/>
403+
<tag name="doctrine.event_listener" event="preUpdate"/>
404+
</service>
405+
</services>
404406
</container>
405407
406408
.. code-block:: php

0 commit comments

Comments
 (0)