1

在 UrlMatcher 类的帮助下,我在解析 URL 字符串并从中获取参数时遇到问题。

资源:

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

4

1 回答 1

-1

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

preg_match_all('!\d+!', $response->getTargetUrl(), $matches);
$id = implode('', $matches[0]);
var_dump($id);
于 2013-03-15T13:21:52.553 回答