【问题标题】:symfony route fails to match its own generated urisymfony 路由无法匹配自己生成的 uri
【发布时间】: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


    【解决方案1】:

    是的,您只在 https 上感到难过的问题,但是如果您检查 HTTP_HOST 的值,则不会说它是 http 或 https。我敢肯定,它会在没有 https 限制的情况下工作

    【讨论】:

    • 是的,它可以不受限制地工作。但这不是我想要/需要的。
    • 无法编辑我之前关于 5 分钟规则的评论原因。在编辑所述评论时,我理解您的意思并对其进行了测试。现在可以使用了!
    猜你喜欢
    • 1970-01-01
    • 2022-11-18
    • 2020-01-08
    • 2016-01-16
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多