【发布时间】:2014-06-18 13:06:53
【问题描述】:
我使用 Symfony 路由组件定义了一个路由
使用这条路线,我生成了一个 uri。 'https://domain.tl/login'
在导航到该 uri 时,UrlMatcher 无法匹配并抛出。
// $_SERVER['REQUEST_URI'] == 'https://domain.tl/login'
// old situation
//$request_context = new RequestContext($_SERVER['REQUEST_URI']);
// problem solved:
$protocol = $_SERVER['SERVER_PORT'] == 80 ? 'http':'https';
$request_context = new RequestContext($_SERVER['REQUEST_URI']);
$request_context->setScheme($protocol);
$routes = new RouteCollection;
// should match: https://domain.tl/login
$routes->add('login', new Route('login', array(), array(), array(), '', array('https')));
// reverse routing
$base_uri = new RequestContext;
$base_uri->setHost($_SERVER['HTTP_HOST']);
$generator = new UrlGenerator($routes, $base_uri);
var_dump($generator->generate('login')); // gives: https://domain.tl/login
try
{
$matcher = new UrlMatcher($routes, $request_context);
$matcher->match($_SERVER['REQUEST_URI']); // throws
die('match');
}
catch(\Symfony\Component\Routing\Exception\ResourceNotFoundException $ex)
{
die('no match');
}
我真的很难弄清楚如何在不深入研究路由组件代码的情况下进行调试。 希望我遗漏了一些明显的东西:)
edit:该路由确实可以在不受 https 限制的情况下工作。不过这个限制是必须的。
edit2:有效!我在上面的代码中实现了修复
【问题讨论】:
标签: php symfony reverse routes