Skip to content

Commit 99df757

Browse files
Spomkyjaviereguiluz
authored andcommitted
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 dec8ce2 commit 99df757

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
@@ -623,8 +623,8 @@ For example, imagine you're processing a :doc:`form </forms>` submission::
623623

624624
.. _request-object-info:
625625

626-
The Request and Response Object
627-
-------------------------------
626+
The Request Object
627+
------------------
628628

629629
As mentioned :ref:`earlier <controller-request-argument>`, Symfony will
630630
pass the ``Request`` object to any controller argument that is type-hinted with
@@ -660,6 +660,36 @@ the ``Request`` class::
660660
The ``Request`` class has several public properties and methods that return any
661661
information you need about the request.
662662

663+
For example, the method ``getPreferredLanguage`` accepts an array of preferred languages and
664+
returns the best language for the user, based on the ``Accept-Language`` header.
665+
The locale is returned with language, script and region, if available (e.g. ``en_US``, ``fr_Latn_CH`` or ``pt``).
666+
667+
Before Symfony 7.1, this method had the following logic:
668+
669+
1. If no locale is set as a parameter, it returns the first language in the
670+
``Accept-Language`` header or ``null`` if the header is empty
671+
2. If no ``Accept-Language`` header is set, it returns the first locale passed
672+
as a parameter.
673+
3. If a locale is set as a parameter and in the ``Accept-Language`` header,
674+
it returns the first exact match.
675+
4. Then, it returns the first language passed in the ``Accept-Language`` header or as argument.
676+
677+
Starting from Symfony 7.1, the method has an additional step:
678+
679+
1. If no locale is set as a parameter, it returns the first language in the
680+
``Accept-Language`` header or ``null`` if the header is empty
681+
2. If no ``Accept-Language`` header is set, it returns the first locale passed
682+
as a parameter.
683+
3. If a locale is set as a parameter and in the ``Accept-Language`` header,
684+
it returns the first exact match.
685+
4. If a language matches the locale, but has a different script or region, it returns the first language in the
686+
``Accept-Language`` header that matches the locale's language, script or region combination
687+
(e.g. ``fr_CA`` will match ``fr_FR``).
688+
5. Then, it returns the first language passed in the ``Accept-Language`` header or as argument.
689+
690+
The Response Object
691+
-------------------
692+
663693
Like the ``Request``, the ``Response`` object has a public ``headers`` property.
664694
This object is of the type :class:`Symfony\\Component\\HttpFoundation\\ResponseHeaderBag`
665695
and provides methods for getting and setting response headers. The header names are

0 commit comments

Comments
 (0)