dongyi9330 2014-06-18 13:06 采纳率: 100%
浏览 32
已采纳

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

I define a route using the Symfony Routing Component

Using this route i generate an uri. 'https://domain.tl/login'

Upon navigating to that uri, the UrlMatcher fails to match and throws.

// $_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');
}

I really struggle figuring out how to debug this without diving in the Routing Component code. Hope i am missing something obvious :)

edit: The route does work without the restriction on https. This restriction is needed though.

edit2: it works! i implemented the fix in the code above

  • 写回答

1条回答 默认 最新

  • doukun1450 2014-06-18 13:12
    关注

    Yep, the problem you sad only on https, but if you check the value for the HTTP_HOST that don't say it's a http or https. I'm sure, it will work without the https restriction

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探