Reputation: 603
Reading symfony/Routing readme:
// ...
$context = new RequestContext();
// this is optional and can be done without a Request instance
$context->fromRequest(Request::createFromGlobals());
$matcher = new UrlMatcher($routes, $context);
What does it mean it's optional? Matcher functionality is somehow limited without it? How Request object is used by matcher?
EDIT
I found that RouteListener takes care of updating context with current request information (host, method etc.). So this optional step is not needed when route matching is done via event dispatcher.
Upvotes: 2
Views: 5520
Reputation: 7808
When creating a new RequestContext, the constructor takes the following arguments, with the following defaults if you don't.
$baseUrl = ''
$method = 'GET'
$host = 'localhost'
$scheme = 'http'
$httpPort = 80
$httpsPort = 443
$path = '/'
However, the RequestContext object can retrieve these values from the HttpFoundation\Request object if one is given. I believe this is new to 2.1 because it's not mentioned in the 2.0 API documentation
You can generate your own Context from a custom Request object rather than using one created from PHP Globals
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\HttpFoundation\Request;
$context = new RequestContext();
$context->fromRequest(Request::create('?foo=1&bar=2'));
$matcher = new UrlMatcher($routes, $context);
or directly apply a PHP Global without creating a new Request object
$context = new RequestContext($_SERVER['REQUEST_URI']);
Upvotes: 1