Skip to content

Updated controller/* articles to Symfony 4 #8664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions controller/argument_value_resolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ Symfony ships with five value resolvers in the HttpKernel component:
argument list. When the action is called, the last (variadic) argument will
contain all the values of this array.

.. note::

Prior to Symfony 3.1, this logic was resolved within the ``ControllerResolver``.
The old functionality is rewritten to the aforementioned value resolvers.

Adding a Custom Value Resolver
------------------------------

Expand All @@ -62,7 +57,7 @@ controller::

class UserController
{
public function indexAction(User $user)
public function index(User $user)
{
return new Response('Hello '.$user->getUsername().'!');
}
Expand Down
2 changes: 1 addition & 1 deletion controller/csrf_token_validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ want to use the Symfony Form component. If, for example, you are implementing
a DELETE action, you can use the :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::isCsrfTokenValid`
method to check the validity of a CSRF token::

public function deleteAction()
public function delete()
{
if ($this->isCsrfTokenValid('token_id', $submittedToken)) {
// ... do something, like deleting an object
Expand Down
60 changes: 27 additions & 33 deletions controller/error_pages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,33 +67,32 @@ logic to determine the template filename:

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

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

.. code-block:: text

app/
└─ Resources/
templates/
└─ bundles/
└─ TwigBundle/
└─ views/
└─ Exception/
├─ error404.html.twig
├─ error403.html.twig
├─ error.html.twig # All other HTML errors (including 500)
├─ error404.json.twig
├─ error403.json.twig
└─ error.json.twig # All other JSON errors (including 500)
└─ Exception/
├─ error404.html.twig
├─ error403.html.twig
├─ error.html.twig # All other HTML errors (including 500)
├─ error404.json.twig
├─ error403.json.twig
└─ error.json.twig # All other JSON errors (including 500)

Example 404 Error Template
--------------------------

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

.. code-block:: html+twig

{# app/Resources/TwigBundle/views/Exception/error404.html.twig #}
{# templates/bundles/TwigBundle/Exception/error404.html.twig #}
{% extends 'base.html.twig' %}

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

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

.. configuration-block::

.. code-block:: yaml

# app/config/routing_dev.yml
# config/routes/dev/twig.yaml
_errors:
resource: "@TwigBundle/Resources/config/routing/errors.xml"
resource: '@TwigBundle/Resources/config/routing/errors.xml'
prefix: /_error

.. code-block:: xml

<!-- app/config/routing_dev.xml -->
<!-- config/routes/dev/twig.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand All @@ -166,7 +166,7 @@ To use this feature, you need to have a definition in your

.. code-block:: php

// app/config/routing_dev.php
// config/routes/dev/twig.php
use Symfony\Component\Routing\RouteCollection;

$collection = new RouteCollection();
Expand All @@ -177,20 +177,14 @@ To use this feature, you need to have a definition in your

return $collection;

If you're coming from an older version of Symfony, you might need to
add this to your ``routing_dev.yml`` file. If you're starting from
scratch, the `Symfony Standard Edition`_ already contains it for you.

With this route added, you can use URLs like
With this route added, you can use URLs like these to preview the *error* page
for a given status code as HTML or for a given status code and format.

.. code-block:: text

http://localhost/index.php/_error/{statusCode}
http://localhost/index.php/_error/{statusCode}.{format}

to preview the *error* page for a given status code as HTML or for a
given status code and format.

.. _custom-exception-controller:
.. _replacing-the-default-exceptioncontroller:

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

.. code-block:: yaml

# app/config/config.yml
# config/packages/twig.yaml
twig:
exception_controller: AppBundle:Exception:showException
exception_controller: App\Controller\ExceptionController::showException

.. code-block:: xml

<!-- app/config/config.xml -->
<!-- config/packages/twig.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand All @@ -226,16 +220,16 @@ configuration option to point to it:
http://symfony.com/schema/dic/twig/twig-1.0.xsd">

<twig:config>
<twig:exception-controller>AppBundle:Exception:showException</twig:exception-controller>
<twig:exception-controller>App\Controller\ExceptionController::showException</twig:exception-controller>
</twig:config>

</container>

.. code-block:: php

// app/config/config.php
// config/packages/twig.php
$container->loadFromExtension('twig', array(
'exception_controller' => 'AppBundle:Exception:showException',
'exception_controller' => 'App\Controller\ExceptionController::showException',
// ...
));

Expand Down
8 changes: 4 additions & 4 deletions controller/forwarding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ sub-request and calls the defined controller. The ``forward()`` method returns
the :class:`Symfony\\Component\\HttpFoundation\\Response` object that is returned
from *that* controller::

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

public function fancyAction($name, $color)
public function fancy($name, $color)
{
// ... create and return a Response object
}

Just like when creating a controller for a route, the order of the arguments
of ``fancyAction()`` doesn't matter: the matching is done by name.
of the ``fancy()`` method doesn't matter: the matching is done by name.
18 changes: 8 additions & 10 deletions controller/service.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ Referencing your Service from Routing
Registering your controller as a service is great, but you also need to make sure
that your routing references the service properly, so that Symfony knows to use it.

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

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

.. configuration-block::

Expand All @@ -31,8 +31,6 @@ syntax:
// You need to use Sensio's annotation to specify a service id
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
// ...

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

/**
* @Route(service="app.hello_controller")
Expand Down Expand Up @@ -73,8 +71,8 @@ syntax:

.. note::

You cannot drop the ``Action`` part of the method name when using the
single colon notation.
When using the ``service_id:method_name`` syntax, the method name must
end with the ``Action`` suffix.

.. _controller-service-invoke:

Expand Down Expand Up @@ -114,7 +112,7 @@ service and use it directly::
$this->twig = $twig;
}

public function indexAction($name)
public function index($name)
{
$content = $this->twig->render(
'hello/index.html.twig',
Expand Down
8 changes: 4 additions & 4 deletions controller/soap_web_service.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ the :ref:`default services configuration <service-container-services-load-exampl
you don't need to do anything!

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

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

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

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

Expand Down
24 changes: 13 additions & 11 deletions controller/upload_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ to :doc:`customize form rendering </form/form_customization>`):
Finally, you need to update the code of the controller that handles the form::

// src/Controller/ProductController.php
namespace App\ProductController;
namespace App\Controller;

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

.. code-block:: yaml

# app/config/config.yml
# config/services.yaml

# ...
parameters:
Expand Down Expand Up @@ -294,7 +294,7 @@ Now you're ready to use this service in the controller::
use App\Service\FileUploader;

// ...
public function newAction(Request $request, FileUploader $fileUploader)
public function new(Request $request, FileUploader $fileUploader)
{
// ...

Expand Down Expand Up @@ -393,14 +393,16 @@ Now, register this class as a Doctrine listener:
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">

<!-- ... be sure autowiring is enabled -->
<defaults autowire="true" />
<!-- ... -->
<services>
<!-- ... be sure autowiring is enabled -->
<defaults autowire="true" />
<!-- ... -->

<service id="App\EventListener\BrochureUploaderListener">
<tag name="doctrine.event_listener" event="prePersist"/>
<tag name="doctrine.event_listener" event="preUpdate"/>
</service>
<service id="App\EventListener\BrochureUploaderListener">
<tag name="doctrine.event_listener" event="prePersist"/>
<tag name="doctrine.event_listener" event="preUpdate"/>
</service>
</services>
</container>

.. code-block:: php
Expand Down