解析URL-String并从中获取参数

时间:2013-03-15 09:45:06

标签: php symfony silex

在UrlMatcher类的帮助下,我遇到了解析URL-String并从中获取参数的问题。

来源:

<?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-String并从中获取参数?

1 个答案:

答案 0 :(得分:-1)

这不太理想,但您可以从字符串中提取数字:

preg_match_all('!\d+!', $response->getTargetUrl(), $matches);
$id = implode('', $matches[0]);
var_dump($id);