Skip to content

Commit 60dd167

Browse files
committed
Merge branch '6.4' into 7.0
* 6.4: Tweaks [HttpFoundation] Mention Request Matchers
2 parents 8c9d5a0 + a9b1372 commit 60dd167

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

components/http_foundation.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,48 @@ use the ``isPrivateIp()`` method from the
379379
$isPrivate = IpUtils::isPrivateIp($ipv6);
380380
// $isPrivate = false
381381

382+
Matching a Request Against a Set of Rules
383+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
384+
385+
The HttpFoundation component provides some matcher classes that allow you to
386+
check if a given request meets certain conditions (e.g. it comes from some IP
387+
address, it uses a certain HTTP method, etc.):
388+
389+
* :class:`Symfony\\Component\\HttpFoundation\\RequestMatcher\\AttributesRequestMatcher`
390+
* :class:`Symfony\\Component\\HttpFoundation\\RequestMatcher\\ExpressionRequestMatcher`
391+
* :class:`Symfony\\Component\\HttpFoundation\\RequestMatcher\\HostRequestMatcher`
392+
* :class:`Symfony\\Component\\HttpFoundation\\RequestMatcher\\IpsRequestMatcher`
393+
* :class:`Symfony\\Component\\HttpFoundation\\RequestMatcher\\IsJsonMatcher`
394+
* :class:`Symfony\\Component\\HttpFoundation\\RequestMatcher\\MethodRequestMatcher`
395+
* :class:`Symfony\\Component\\HttpFoundation\\RequestMatcher\\PathRequestMatcher`
396+
* :class:`Symfony\\Component\\HttpFoundation\\RequestMatcher\\PortRequestMatcher`
397+
* :class:`Symfony\\Component\\HttpFoundation\\RequestMatcher\\SchemeRequestMatcher`
398+
399+
You can use them individually or combine them using the
400+
:class:`Symfony\\Component\\HttpFoundation\\ChainRequestMatcher`
401+
class::
402+
403+
use Symfony\Component\HttpFoundation\ChainRequestMatcher;
404+
use Symfony\Component\HttpFoundation\RequestMatcher\HostRequestMatcher;
405+
use Symfony\Component\HttpFoundation\RequestMatcher\PathRequestMatcher;
406+
use Symfony\Component\HttpFoundation\RequestMatcher\SchemeRequestMatcher;
407+
408+
// use only one criteria to match the request
409+
$schemeMatcher = new SchemeRequestMatcher('https');
410+
if ($schemeMatcher->matches($request)) {
411+
// ...
412+
}
413+
414+
// use a set of criteria to match the request
415+
$matcher = new ChainRequestMatcher([
416+
new HostRequestMatcher('example.com'),
417+
new PathRequestMatcher('/admin'),
418+
]);
419+
420+
if ($matcher->matches($request)) {
421+
// ...
422+
}
423+
382424
Accessing other Data
383425
~~~~~~~~~~~~~~~~~~~~
384426

0 commit comments

Comments
 (0)