Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.2k
Final additions: Expression language framework docs#3258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
ac15bf5
be86fef
d72759a
33676dc
0cf1828
bc22f16
0000250
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -507,10 +507,11 @@ to the ``{page}`` parameter. | ||
| /blog/my-blog-post | blog | {page} = my-blog-post | | ||
+--------------------+-------+-----------------------+ | ||
The answer to the problem is to add route *requirements* or route *conditions* | ||
(see :ref:`book-routing-conditions`). The routes in this example would work | ||
perfectly if the ``/blog/{page}`` path *only* matched URLs where the ``{page}`` | ||
portion is an integer. Fortunately, regular expression requirements can easily | ||
be added for each parameter. For example: | ||
.. configuration-block:: | ||
@@ -717,6 +718,95 @@ You can also match on the HTTP *host* of the incoming request. For more | ||
information, see :doc:`/components/routing/hostname_pattern` in the Routing | ||
component documentation. | ||
.. _book-routing-conditions: | ||
Completely Customized Route Matching with Conditions | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
.. versionadded:: 2.4 | ||
Route conditions were introduced in Symfony 2.4. | ||
As you've seen, a route can be made to match only certain routing wildcards | ||
(via regular expressions), HTTP methods, or host names. But the routing system | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. actually, they are not regular expressions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. hmm, maybe the sentence is confusing :) "to match only certain routing wildcards" I thought you meaned the | ||
can be extended to have an almost infinite flexibility using ``conditions``: | ||
.. configuration-block:: | ||
.. code-block:: yaml | ||
contact: | ||
path: /contact | ||
defaults: { _controller: AcmeDemoBundle:Main:contact } | ||
condition: "context.getMethod() in ['GET', 'HEAD'] and request.headers.get('User-Agent') matches '/firefox/i'" | ||
.. code-block:: xml | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<routes xmlns="http://symfony.com/schema/routing" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/routing | ||
http://symfony.com/schema/routing/routing-1.0.xsd"> | ||
<route id="contact" | ||
path="/contact" | ||
condition="context.getMethod() in ['GET', 'HEAD'] and request.headers.get('User-Agent') matches '/firefox/i'" | ||
> | ||
<default key="_controller">AcmeDemoBundle:Main:contact</default> | ||
</route> | ||
</routes> | ||
.. code-block:: php | ||
use Symfony\Component\Routing\RouteCollection; | ||
use Symfony\Component\Routing\Route; | ||
$collection = new RouteCollection(); | ||
$collection->add('contact', new Route( | ||
'/contact', array( | ||
'_controller' => 'AcmeDemoBundle:Main:contact', | ||
), | ||
array(), | ||
array(), | ||
'', | ||
array(), | ||
array(), | ||
'context.getMethod() in ["GET", "HEAD"] and request.headers.get("User-Agent") matches "/firefox/i"' | ||
)); | ||
return $collection; | ||
The ``condition`` is an expression, and you can learn more about its syntax | ||
here: :doc:`/components/expression_language/syntax`. With this, the route | ||
won't match unless the HTTP method is either GET or HEAD *and* if the ``User-Agent`` | ||
header matches ``firefox``. | ||
You can do any complex logic you need in the expression by leveraging two | ||
variables that are passed into the expression: | ||
* ``context``: An instance of :class:`Symfony\\Component\\Routing\\RequestContext`, | ||
which holds the most fundamental information about the route being matched; | ||
* ``request``: The Symfony :class:`Symfony\\Component\\HttpFoundation\\Request`` | ||
object (see :ref:`component-http-foundation-request`). | ||
.. caution:: | ||
Conditions are *not* taken into account when generating a URL. | ||
.. sidebar:: Expressions are Compiled to PHP | ||
Behind the scenes, expressions are compiled down to raw PHP. Our example | ||
would generate the following PHP in the cache directory:: | ||
if (rtrim($pathinfo, '/contact') === '' && ( | ||
in_array($context->getMethod(), array(0 => "GET", 1 => "HEAD")) | ||
&& preg_match("/firefox/i", $request->headers->get("User-Agent")) | ||
)) { | ||
// ... | ||
} | ||
Because of this, using the ``condition`` key causes no extra overhead | ||
beyond the time it takes for the underlying PHP to execute. | ||
.. index:: | ||
single: Routing; Advanced example | ||
single: Routing; _format parameter | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
.. index:: | ||
single: Expressions in the Framework | ||
How to use Expressions in Security, Routing, Services, and Validation | ||
===================================================================== | ||
.. versionadded:: 2.4 | ||
The expression functionality was introduced in Symfony 2.4. | ||
In Symfony 2.4, a powerful :doc:`ExpressionLanguage </components/expression_language/introduction>` | ||
component was added to Symfony. This allows us to add highly customized | ||
logic inside configuration. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. wonder if we should add a note on how to use or inject expressions, if need be, to other services besides these explicit here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I think over time we will start to think of other uses for the expression language, and when we do, we should certainly add more cookbook entries. But fortunately, for right now, we just need to make sure the basic docs are present. | ||
The Symfony Framework leverages expressions out of the box in the following | ||
ways: | ||
* :ref:`Configuring services <book-services-expressions>`; | ||
* :ref:`Route matching conditions <book-routing-conditions>`; | ||
* :ref:`Checking security <book-security-expressions>` and | ||
:ref:`access controls with allow_if <book-security-allow-if>`; | ||
* :doc:`Validation </reference/constraints/Expression>`. | ||
For more information about how to create and work with expressions, see | ||
:doc:`/components/expression_language/syntax`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. php-cs-fixer |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Expressions | ||
=========== | ||
.. toctree:: | ||
:maxdepth: 2 | ||
expressions |
Uh oh!
There was an error while loading.Please reload this page.