Skip to content

Commit b76f69f

Browse files
committed
Refactor Request object documentation and redefine method logic
The documentation for the Request object in the controller documentation has been updated to be more specific. The logic of the `getPreferredLanguage` method has been updated and clarified in Symfony 7.1. An additional step has been added which accounts for languages that match the locale, but exhibit a different script or region. The Response Object section has been moved for better flow and readability.
1 parent 5327456 commit b76f69f

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

controller.rst

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,8 +596,8 @@ For example, imagine you're processing a :doc:`form </forms>` submission::
596596

597597
.. _request-object-info:
598598

599-
The Request and Response Object
600-
-------------------------------
599+
The Request Object
600+
------------------
601601

602602
As mentioned :ref:`earlier <controller-request-argument>`, Symfony will
603603
pass the ``Request`` object to any controller argument that is type-hinted with
@@ -633,6 +633,36 @@ the ``Request`` class::
633633
The ``Request`` class has several public properties and methods that return any
634634
information you need about the request.
635635

636+
For example, the method ``getPreferredLanguage`` accepts an array of preferred languages and
637+
returns the best language for the user, based on the ``Accept-Language`` header.
638+
The locale is returned with language, script and region, if available (e.g. ``en_US``, ``fr_Latn_CH`` or ``pt``).
639+
640+
Before Symfony 7.1, this method had the following logic:
641+
642+
1. If no locale is set as a parameter, it returns the first language in the
643+
``Accept-Language`` header or ``null`` if the header is empty
644+
2. If no ``Accept-Language`` header is set, it returns the first locale passed
645+
as a parameter.
646+
3. If a locale is set as a parameter and in the ``Accept-Language`` header,
647+
it returns the first exact match.
648+
4. Then, it returns the first language passed in the ``Accept-Language`` header or as argument.
649+
650+
Starting from Symfony 7.1, the method has an additional step:
651+
652+
1. If no locale is set as a parameter, it returns the first language in the
653+
``Accept-Language`` header or ``null`` if the header is empty
654+
2. If no ``Accept-Language`` header is set, it returns the first locale passed
655+
as a parameter.
656+
3. If a locale is set as a parameter and in the ``Accept-Language`` header,
657+
it returns the first exact match.
658+
4. If a language matches the locale, but has a different script or region, it returns the first language in the
659+
``Accept-Language`` header that matches the locale's language, script or region combination
660+
(e.g. ``fr_CA`` will match ``fr_FR``).
661+
5. Then, it returns the first language passed in the ``Accept-Language`` header or as argument.
662+
663+
The Response Object
664+
-------------------
665+
636666
Like the ``Request``, the ``Response`` object has a public ``headers`` property.
637667
This object is of the type :class:`Symfony\\Component\\HttpFoundation\\ResponseHeaderBag`
638668
and provides methods for getting and setting response headers. The header names are

0 commit comments

Comments
 (0)