Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
weaverryan merged 1 commit intosymfony:masterfromjaviereguiluz:update_controller
Nov 17, 2017
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionscontroller/argument_value_resolver.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 functionindexAction(User $user)
public functionindex(User $user)
{
return new Response('Hello '.$user->getUsername().'!');
}
Expand Down
2 changes: 1 addition & 1 deletioncontroller/csrf_token_validation.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 functiondeleteAction()
public functiondelete()
{
if ($this->isCsrfTokenValid('token_id', $submittedToken)) {
// ... do something, like deleting an object
Expand Down
60 changes: 27 additions & 33 deletionscontroller/error_pages.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionscontroller/forwarding.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 functionindexAction($name)
public functionindex($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 functionfancyAction($name, $color)
public functionfancy($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.
ofthe ``fancy()`` method doesn't matter: the matching is done by name.
18 changes: 8 additions & 10 deletionscontroller/service.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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'redone! You can use the normal ``App\Controller\HelloController::index``
syntax in your routing andit 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 DownExpand Up@@ -73,8 +71,8 @@ syntax:

.. note::

You cannot dropthe ``Action``part ofthe method namewhen using the
single colon notation.
When usingthe ``service_id:method_name``syntax,the method namemust
end with the ``Action`` suffix.

.. _controller-service-invoke:

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

public functionindexAction($name)
public functionindex($name)
{
$content = $this->twig->render(
'hello/index.html.twig',
Expand Down
8 changes: 4 additions & 4 deletionscontroller/soap_web_service.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 functionindexAction(HelloService $helloService)
public functionindex(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()``methodin the controller above is accessible via
theroute ``/soap``::

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

Expand Down
24 changes: 13 additions & 11 deletionscontroller/upload_file.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 functionnewAction(Request $request)
public functionnew(Request $request)
{
$product = new Product();
$form = $this->createForm(ProductType::class, $product);
Expand DownExpand 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 DownExpand Up@@ -294,7 +294,7 @@ Now you're ready to use this service in the controller::
use App\Service\FileUploader;

// ...
public functionnewAction(Request $request, FileUploader $fileUploader)
public functionnew(Request $request, FileUploader $fileUploader)
{
// ...

Expand DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp