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

Commit767fa40

Browse files
committed
[FrameworkBundle] Document the AbstractController
1 parent01b10f1 commit767fa40

File tree

1 file changed

+77
-10
lines changed

1 file changed

+77
-10
lines changed

‎controller/service.rst‎

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ These are the main **advantages** of defining controllers as services:
2929
These are the main **drawbacks** of defining controllers as services:
3030

3131
* It takes more work to create the controllers because they don't have
32-
automatic access to the services or to the base controller shortcuts;
32+
automatic access to the services;
3333
* The constructor of the controllers can rapidly become too complex because you
3434
must inject every single dependency needed by them;
35-
* The code of the controllers is more verbose because you can't use the shortcuts
36-
of the base controller and you must replace them with some lines of code.
3735

3836
The recommendation from the:doc:`best practices</best_practices/controllers>`
3937
is also valid for controllers defined as services: avoid putting your business
@@ -83,6 +81,74 @@ Then you can define it as a service as follows:
8381
8482
$container->register('app.hello_controller', HelloController::class);
8583
84+
Using the controller shortcuts
85+
------------------------------
86+
87+
To use the traditional controller shortcuts, you can use the
88+
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller` class if you
89+
need to access the container or the
90+
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController` class
91+
if you prefer explicit injection (you can't access the container).
92+
93+
The example above could be revamped to::
94+
95+
// src/AppBundle/Controller/HelloController.php
96+
namespace AppBundle\Controller;
97+
98+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
99+
100+
class HelloController extends AbstractController
101+
{
102+
public function indexAction($name)
103+
{
104+
// renders app/Resources/views/hello/index.html.twig
105+
return $this->render('hello/index.html.twig', array('name' => $name));
106+
}
107+
}
108+
109+
With the following configuration:
110+
111+
..configuration-block::
112+
113+
..code-block::yaml
114+
115+
# app/config/services.yml
116+
services:
117+
_instanceof:
118+
Symfony\Bundle\FrameworkBundle\Controller\AbstractController:
119+
calls:[ [ setContainer, [ '@container' ] ] ]
120+
tags:[ container.service_subscriber ]
121+
122+
AppBundle\Controller\HelloController:~
123+
124+
..code-block::xml
125+
126+
<!-- app/config/services.xml-->
127+
<services>
128+
<instanceofid="Symfony\Bundle\FrameworkBundle\Controller\AbstractController">
129+
<callmethod="setController">
130+
<argumenttype="service"id="container" />
131+
</call>
132+
<tagname="container.service_subscriber" />
133+
</instanceof>
134+
135+
<serviceid="AppBundle\Controller\HelloController" />
136+
</services>
137+
138+
..code-block::php
139+
140+
// app/config/services.php
141+
use AppBundle\Controller\HelloController;
142+
use Symfony\Component\DependencyInjection\Reference;
143+
144+
$container->register(HelloController::class)
145+
->addMethodCall('setContainer', new Reference('container'));
146+
147+
..versionadded::3.3
148+
The:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController`
149+
class was added in Symfony 3.3. Prior to version 3.3, you needed to use the
150+
base ``Controller`` class or use alternatives to its shortcut methods.
151+
86152
Referring to the Service
87153
------------------------
88154

@@ -135,14 +201,15 @@ the route ``_controller`` value:
135201
If your controller implements the ``__invoke()`` method, you can simply
136202
refer to the service id (``app.hello_controller``).
137203

138-
Alternatives tobaseController Methods
139-
---------------------------------------
204+
Alternatives to Controller shortcut Methods
205+
-------------------------------------------
140206

141-
When using a controller defined as a service, it will most likely not extend
142-
the base ``Controller`` class. Instead of relying on its shortcut methods,
143-
you'll interact directly with the services that you need. Fortunately, this is
144-
usually pretty easy and the base `Controller class source code`_ is a great
145-
source on how to perform many common tasks.
207+
In case you don't want to inject the container in your controllers,
208+
it's possible to get ride of controller shortcut methods: you can interact
209+
directly with the services that you need.
210+
Fortunately, this is usually pretty easy and the base
211+
`Controller class source code`_ is a great source on how to perform many common
212+
tasks.
146213

147214
For example, if you want to render a template instead of creating the ``Response``
148215
object directly, then your code would look like this if you were extending

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp