Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3ade0c8

Browse files
committed
Merge branch '4.1'
* 4.1: Use AbstractController in the main controller article Minor rewords Fix broken link to `vue-loader` documentation. Simplify Apache config with FallbackResource Made the code of a validation example more robust Tell about controllers in doc about service subscribers
2 parents e22943b + baa1ea8 commit 3ade0c8

File tree

7 files changed

+56
-67
lines changed

7 files changed

+56
-67
lines changed

controller.rst

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ For more information on routing, see :doc:`/routing`.
8686
single: Controller; Base controller class
8787

8888
.. _the-base-controller-class-services:
89+
.. _the-base-controller-classes-services:
8990

90-
The Base Controller Classes & Services
91-
--------------------------------------
91+
The Base Controller Class & Services
92+
------------------------------------
9293

93-
To make life nicer, Symfony comes with two optional base
94-
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller` and
94+
To make life nicer, Symfony comes with an optional base controller class called
9595
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController`.
96-
You can extend either to get access to some `helper methods`_.
96+
You can extend it to get access to some `helper methods`_.
9797

9898
Add the ``use`` statement atop your controller class and then modify
9999
``LuckyController`` to extend it:
@@ -103,35 +103,24 @@ Add the ``use`` statement atop your controller class and then modify
103103
// src/Controller/LuckyController.php
104104
namespace App\Controller;
105105
106-
+ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
106+
+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
107107
108108
- class LuckyController
109-
+ class LuckyController extends Controller
109+
+ class LuckyController extends AbstractController
110110
{
111111
// ...
112112
}
113113
114114
That's it! You now have access to methods like :ref:`$this->render() <controller-rendering-templates>`
115115
and many others that you'll learn about next.
116116

117-
.. _controller-abstract-versus-controller:
118-
119-
.. tip::
120-
121-
What's the difference between ``Controller`` or ``AbstractController``? Not much:
122-
both are identical, except that ``AbstractController`` is more restrictive: it
123-
does not allow you to access services directly via ``$this->get()`` or
124-
``$this->container->get()``. This forces you to write more robust code to access
125-
services. But if you *do* need direct access to the container, using ``Controller``
126-
is fine.
127-
128117
.. index::
129118
single: Controller; Redirecting
130119

131120
Generating URLs
132121
~~~~~~~~~~~~~~~
133122

134-
The :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::generateUrl`
123+
The :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\ControllerTrait::generateUrl`
135124
method is just a helper method that generates the URL for a given route::
136125

137126
$url = $this->generateUrl('app_lucky_number', array('max' => 10));
@@ -650,7 +639,7 @@ contains the logic for that page. In Symfony, this is called a controller,
650639
and it's a PHP function where you can do anything in order to return the
651640
final ``Response`` object that will be returned to the user.
652641

653-
To make life easier, you'll probably extend the base ``Controller`` class because
642+
To make life easier, you'll probably extend the base ``AbstractController`` class because
654643
this gives access to shortcut methods (like ``render()`` and ``redirectToRoute()``).
655644

656645
In other articles, you'll learn how to use specific services from inside your controller

controller/service.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ without the method (``App\Controller\HelloController`` for example).
8181
Alternatives to base Controller Methods
8282
---------------------------------------
8383

84-
When using a controller defined as a service, you can still extend any of the
85-
:ref:`normal base controller <the-base-controller-class-services>` classes and
86-
use their shortcuts. But, you don't need to! You can choose to extend *nothing*,
84+
When using a controller defined as a service, you can still extend the
85+
:ref:`AbstractController base controller <the-base-controller-class-services>`
86+
and use its shortcuts. But, you don't need to! You can choose to extend *nothing*,
8787
and use dependency injection to access different services.
8888

8989
The base `Controller class source code`_ is a great way to see how to accomplish

frontend/encore/vuejs.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ also configure the `vue-loader options`_ via a callback:
2727
.. code-block:: javascript
2828
2929
.enableVueLoader(function(options) {
30-
// https://vue-loader.vuejs.org/en/configurations/advanced.html
30+
// https://vue-loader.vuejs.org/options.html
3131
3232
options.preLoaders = {
3333
js: '/path/to/custom/loader'
@@ -53,4 +53,4 @@ See :doc:`/frontend/encore/dev-server` for more details.
5353

5454
.. _`babel-preset-react`: https://babeljs.io/docs/plugins/preset-react/
5555
.. _`Vue.js`: https://vuejs.org/
56-
.. _`vue-loader options`: https://vue-loader.vuejs.org/en/configurations/advanced.html
56+
.. _`vue-loader options`: https://vue-loader.vuejs.org/options.html

service_container/3.3-di-changes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ The third big change is that, in a new Symfony 3.3 project, your controllers are
326326
$this->registerClasses($definition, 'App\\Controller\\', '../src/Controller/*');
327327
328328
But, you might not even notice this. First, your controllers *can* still extend
329-
the same base ``Controller`` class or a new :ref:`AbstractController <controller-abstract-versus-controller>`.
329+
the same base controller class (``AbstractController``).
330330
This means you have access to all of the same shortcuts as before. Additionally,
331331
the ``@Route`` annotation and ``_controller`` syntax (e.g. ``App:Default:homepage``)
332332
used in routing will automatically use your controller as a service (as long as its

service_container/service_subscribers_locators.rst

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ instantiation of the services to be lazy. However, that's not possible using
1313
the explicit dependency injection since services are not all meant to
1414
be ``lazy`` (see :doc:`/service_container/lazy_services`).
1515

16-
A real-world example are applications that implement the `Command pattern`_
16+
This can typically be the case in your controllers, where you may inject several
17+
services in the constructor, but the action executed only uses some of them.
18+
Another example are applications that implement the `Command pattern`_
1719
using a CommandBus to map command handlers by Command class names and use them
1820
to handle their respective command when it is asked for::
1921

@@ -50,35 +52,12 @@ to handle their respective command when it is asked for::
5052

5153
Considering that only one command is handled at a time, instantiating all the
5254
other command handlers is unnecessary. A possible solution to lazy-load the
53-
handlers could be to inject the whole dependency injection container::
54-
55-
// ...
56-
use Symfony\Component\DependencyInjection\ContainerInterface;
57-
58-
class CommandBus
59-
{
60-
private $container;
61-
62-
public function __construct(ContainerInterface $container)
63-
{
64-
$this->container = $container;
65-
}
66-
67-
public function handle(Command $command)
68-
{
69-
$commandClass = get_class($command);
70-
71-
if ($this->container->has($commandClass)) {
72-
$handler = $this->container->get($commandClass);
73-
74-
return $handler->handle($command);
75-
}
76-
}
77-
}
55+
handlers could be to inject the main dependency injection container.
7856

7957
However, injecting the entire container is discouraged because it gives too
8058
broad access to existing services and it hides the actual dependencies of the
81-
services.
59+
services. Doing so also requires services to be made public, which isn't the
60+
case by default in Symfony applications.
8261

8362
**Service Subscribers** are intended to solve this problem by giving access to a
8463
set of predefined services while instantiating them only when actually needed
@@ -139,8 +118,7 @@ The injected service is an instance of :class:`Symfony\\Component\\DependencyInj
139118
which implements the PSR-11 ``ContainerInterface``, but it is also a callable::
140119

141120
// ...
142-
$locateHandler = $this->locator;
143-
$handler = $locateHandler($commandClass);
121+
$handler = ($this->locator)($commandClass);
144122

145123
return $handler->handle($command);
146124

@@ -173,6 +151,24 @@ Service types can also be keyed by a service name for internal use::
173151
];
174152
}
175153

154+
When extending a class that also implements ``ServiceSubscriberInterface``,
155+
it's your responsibility to call the parent when overriding the method. This
156+
typically happens when extending ``AbstractController``::
157+
158+
use Psr\Log\LoggerInterface;
159+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
160+
161+
class MyController extends AbstractController
162+
{
163+
public static function getSubscribedServices()
164+
{
165+
return array_merge(parent::getSubscribedServices(), [
166+
// ...
167+
'logger' => LoggerInterface::class,
168+
]);
169+
}
170+
}
171+
176172
Optional Services
177173
~~~~~~~~~~~~~~~~~
178174

setup/web_server_configuration.rst

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,7 @@ and increase web server performance:
9797
Order Allow,Deny
9898
Allow from All
9999
100-
<IfModule mod_rewrite.c>
101-
Options -MultiViews
102-
RewriteEngine On
103-
RewriteCond %{REQUEST_FILENAME} !-f
104-
RewriteRule ^(.*)$ index.php [QSA,L]
105-
</IfModule>
100+
FallbackResource /index.php
106101
</Directory>
107102
108103
# uncomment the following lines if you install assets as symlinks
@@ -111,13 +106,11 @@ and increase web server performance:
111106
# Options FollowSymlinks
112107
# </Directory>
113108
114-
# optionally disable the RewriteEngine for the asset directories
115-
# which will allow apache to simply reply with a 404 when files are
116-
# not found instead of passing the request into the full symfony stack
109+
# optionally disable the fallback resource for the asset directories
110+
# which will allow Apache to return a 404 error when files are
111+
# not found instead of passing the request to Symfony
117112
<Directory /var/www/project/public/bundles>
118-
<IfModule mod_rewrite.c>
119-
RewriteEngine Off
120-
</IfModule>
113+
FallbackResource disabled
121114
</Directory>
122115
ErrorLog /var/log/apache2/project_error.log
123116
CustomLog /var/log/apache2/project_access.log combined

validation/custom_constraint.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,22 @@ The validator class is also simple, and only has one required method ``validate(
5959

6060
use Symfony\Component\Validator\Constraint;
6161
use Symfony\Component\Validator\ConstraintValidator;
62+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
6263

6364
class ContainsAlphanumericValidator extends ConstraintValidator
6465
{
6566
public function validate($value, Constraint $constraint)
6667
{
68+
// custom constraints should ignore null and empty values to allow
69+
// other constraints (NotBlank, NotNull, etc.) take care of that
70+
if (null === $value || '' === $value) {
71+
return;
72+
}
73+
74+
if (!is_string($value)) {
75+
throw new UnexpectedTypeException($value, 'string');
76+
}
77+
6778
if (!preg_match('/^[a-zA-Z0-9]+$/', $value, $matches)) {
6879
$this->context->buildViolation($constraint->message)
6980
->setParameter('{{ string }}', $value)

0 commit comments

Comments
 (0)