Php 解析URL字符串并从中获取参数
在UrlMatcher类的帮助下,我在解析URL字符串并从中获取参数时遇到问题 资料来源:Php 解析URL字符串并从中获取参数,php,symfony,silex,Php,Symfony,Silex,在UrlMatcher类的帮助下,我在解析URL字符串并从中获取参数时遇到问题 资料来源: <?php //RouteCollection $routes = $this->container->get('routes'); //SubRequest to a Controller (POST) $response = $this->forward(...); if($response instanceof RedirectResponse){ //Targe
<?php
//RouteCollection
$routes = $this->container->get('routes');
//SubRequest to a Controller (POST)
$response = $this->forward(...);
if($response instanceof RedirectResponse){
//TargetUrl looks like this
//dev.php/admin/twitter-modul/1/show
//1 is the id of the Entity
$context = new RequestContext($response->getTargetUrl());
$matcher = new UrlMatcher($routes, $context);
//match throws a ResourceNotFoundException
$parameters = $matcher->match($response->getTargetUrl());
//Here i need the id paramater from the url
var_dump($parmeters);
}
return $response;
如何解析URL字符串并从中获取参数?这并不理想,但您可以从字符串中提取数字:
preg_match_all('!\d+!', $response->getTargetUrl(), $matches);
$id = implode('', $matches[0]);
var_dump($id);
我认为OP正在寻找一种Symfony 2方法:但这一种可能有效。啊哈,我100%同意,但我不知道一种Symfony方法:我唯一能想到的是直接从字符串中提取id。我相信有人会找到Sf2的方法。嘿,谢谢你的回复。我没看到谢谢,但这不是问题所在。match方法抛出一个ResourceNotFound异常,这意味着php解释器还没有实现。2这将不起作用,因为targetUrl中没有主机,因此$host将为空。3当然可以,但我想使用symfony组件。