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

Appearance settings

Commit eb9de96

Browse filesBrowse files
committed
Add stateless route attribute documentation
1 parent 1e3df40 commit eb9de96
Copy full SHA for eb9de96

File tree

1 file changed

+71
-0
lines changed
Filter options

1 file changed

+71
-0
lines changed

‎routing.rst

Copy file name to clipboardExpand all lines: routing.rst
+71Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,77 @@ 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+
Routes can configure a ``stateless`` boolean option in order to make sure that the controller
1814+
is not using the session during the request handling.
1815+
1816+
.. configuration-block::
1817+
1818+
.. code-block:: php-annotations
1819+
1820+
// src/Controller/MainController.php
1821+
namespace App\Controller;
1822+
1823+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1824+
use Symfony\Component\Routing\Annotation\Route;
1825+
1826+
class MainController extends AbstractController
1827+
{
1828+
/**
1829+
* @Route("/", name="homepage", stateless=true)
1830+
*/
1831+
public function homepage()
1832+
{
1833+
// ...
1834+
}
1835+
}
1836+
1837+
.. code-block:: yaml
1838+
1839+
# config/routes.yaml
1840+
homepage:
1841+
controller: App\Controller\MainController::homepage
1842+
path: /
1843+
stateless: true
1844+
1845+
.. code-block:: xml
1846+
1847+
<!-- config/routes.xml -->
1848+
<?xml version="1.0" encoding="UTF-8" ?>
1849+
<routes xmlns="http://symfony.com/schema/routing"
1850+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1851+
xsi:schemaLocation="http://symfony.com/schema/routing
1852+
https://symfony.com/schema/routing/routing-1.0.xsd">
1853+
<route id="homepage" controller="App\Controller\MainController::homepage" path="/" stateless="true"/>
1854+
</routes>
1855+
1856+
.. code-block:: php
1857+
1858+
// config/routes.php
1859+
use App\Controller\MainController;
1860+
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
1861+
1862+
return function (RoutingConfigurator $routes) {
1863+
$routes->add('homepage', '/')
1864+
->controller([MainController::class, 'homepage'])
1865+
->stateless()
1866+
;
1867+
};
1868+
1869+
If a stateless declared route is using the session, the application will:
1870+
- Throw an `Symfony\\Component\\HttpKernel\\Exception\\UnexpectedSessionUsageException` when debugging is enabled
1871+
- Log a warning when debugging is disabled.
1872+
1873+
.. tip::
1874+
1875+
When the application uses full "language + territory" locales (e.g. ``fr_FR``,
1876+
``fr_BE``), if the URLs are the same in all related locales, routes can use
1877+
only the language part (e.g. ``fr``) to avoid repeating the same URLs.
1878+
18081879
.. _routing-generating-urls:
18091880

18101881
Generating URLs

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.