symfony route无法匹配自己生成的uri

时间:2014-06-18 13:06:53

标签: php symfony reverse routes

我使用Symfony Routing Component

定义路由

使用此路线我生成一个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');
}

我真的很难弄清楚如何在不使用路由组件代码的情况下调试它。 希望我错过了一些明显的东西:)

编辑:路线确实有效,没有对https的限制。但是需要这个限制。

edit2 :它有效!我在上面的代码中实现了修复

1 个答案:

答案 0 :(得分:0)

是的,这个问题只在https上让您感到难过,但如果您检查HTTP_HOST的值,则不要说它是http或https。我确定,它可以在没有https限制的情况下工作