8000 Add stateless route attribute documentation · symfony/symfony-docs@463c49d · GitHub
[go: up one dir, main page]

Skip to content
Sign in

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

Appearance settings

Commit 463c49d

Browse files
committed
Add stateless route attribute documentation
1 parent 1e3df40 commit 463c49d

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

routing.rst

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,82 @@ with a locale. This can be done by defining a different prefix for each locale
18051805
;
18061806
};
18071807
1808+
.. _stateless-routing:
1809+
1810+
Stateless Routes
1811+
----------------
1812+
1813+
.. versionadded:: 5.1
1814+
1815+
The ``stateless`` option was introduced in Symfony 5.1
1816+
1817+
Sometimes, when an HTTP response should be cached, it is important to ensure
1818+
that can happen. However, whenever session is started during a request, Symfony
1819+
turns the response into a private non-cacheable response.
1820+
1821+
For details, see :doc:`/http_cache`
1822+
1823+
Routes can configure a ``stateless`` boolean option in order to make sure that
1824+
the session won't ever be used when matching a request:
1825+
1826+
.. configuration-block::
1827+
1828+
.. code-block:: php-annotations
1829+
1830+
// src/Controller/MainController.php
1831+
namespace App\Controller;
1832+
1833+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1834+
use Symfony\Component\Routing\Annotation\Route;
1835+
1836+
class MainController extends AbstractController
1837+
{
1838+
/**
1839+
* @Route("/", name="homepage", stateless=true)
1840+
*/
1841+
public function homepage()
1842+
{
1843+
// ...
1844+
}
1845+
}
1846+
1847+
.. code-block:: yaml
1848+
1849+
# config/routes.yaml
1850+
homepage:
1851+
controller: App\Controller\MainController::homepage
1852+
path: /
1853+
stateless: true
1854+
1855+
.. code-block:: xml
1856+
1857+
<!-- config/routes.xml -->
1858+
<?xml version="1.0" encoding="UTF-8" ?>
1859+
<routes xmlns="http://symfony.com/schema/routing"
1860+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1861+
xsi:schemaLocation="http://symfony.com/schema/routing
1862+
https://symfony.com/schema/routing/routing-1.0.xsd">
1863+
<route id="homepage" controller="App\Controller\MainController::homepage" path="/" stateless="true"/>
1864+
</routes>
1865+
1866+
.. code-block:: php
1867+
1868+
// config/routes.php
1869+
use App\Controller\MainController;
1870+
use Symfony\Bundle\FrameworkBundle\Routing\Loader\Configurator\RoutingConfigurator;
1871+
1872+
return function (RoutingConfigurator $routes) {
1873+
$routes->add('homepage', '/')
1874+
->controller([MainController::class, 'homepage'])
1875+
->stateless()
1876+
;
1877+
};
1878+
1879+
Now, if the session is used, the application will report it based on your
1880+
``kernel.debug`` parameter:
1881+
* ``enabled``: will throw an :class:`Symfony\\Component\\HttpKernel\\Exception\\UnexpectedSessionUsageException` exception
1882+
* ``disabled``: will log a warning
1883+
18081884
.. _routing-generating-urls:
18091885

18101886
Generating URLs

0 commit comments

Comments
 (0)
0