简体   繁体   English

Symfony2显式获取URL的路径信息

[英]Symfony2 get path info of URL explicitely

Say, I have an URL http://server/mysite/web/app_dev.php/resource/1 . 说,我有一个URL http://server/mysite/web/app_dev.php/resource/1 I am doing a GET request and the corresponding action is ResourceController::getAction . 我正在执行GET请求,相应的操作是ResourceController::getAction

In this controller action if I call $request->getPathInfo() , it gives me /resource/1 . 在此控制器操作中,如果我调用$request->getPathInfo() ,它将给我/resource/1

But in the same controller if I create a Request object with a url of another resource and call getPathInfo() it returns a longer version. 但是在同一个控制器中,如果我使用另一个资源的url创建一个Request对象并调用getPathInfo()它将返回更长的版本。

$request = Request::create('http://server/mysite/web/app_dev.php/another_resource/1');
echo $request->getPathInfo();

OUTPUT >>
/mysite/web/app_dev.php/another_resource/1

How is it possible to make getPathInfo() to return only /another_resource/1 in this case? 在这种情况下,如何使getPathInfo()仅返回/another_resource/1

OR 要么

Anyone can suggest what is the safest way to convert an endpoint URL http://server/mysite/web/app_dev.php/another_resource/1 to /another_resource/1 in Symfony2? 任何人都可以建议在Symfony2中将端点URL http://server/mysite/web/app_dev.php/another_resource/1转换为/another_resource/1的最安全方法是什么?

In case you are interested to know why I need this 如果您有兴趣知道为什么我需要这个

The controller action is receiving some URLs in request content. 控制器操作正在接收请求内容中的某些URL。 The action needs to parse those URLs to recognize the corresponding resource. 该操作需要解析这些URL以识别相应的资源。 I am trying to make use to $router->match function to retrieve the parameters from the URL. 我试图利用$router->match函数从URL中检索参数。 The match function expects only /another_resource/1 part. 匹配函数只需要/another_resource/1部分。

Request::create method create a new request that don't know any information about current request, it return full uri because it not know current script file. Request :: create方法创建一个不知道有关当前请求的任何信息的新请求,由于不知道当前脚本文件,它返回完整的uri。 try: 尝试:

$request = Request::create('http://server/mysite/web/app_dev.php/another_resource/1', null, array(), array(), array(), array(
    'SCRIPT_NAME' => $this->get('kernel')->getEnvironment() == 'dev' ? 'app_dev.php' : 'app.php',
    'SCRIPT_FILENAME' => $this->get('kernel')->getEnvironment() == 'dev' ? 'app_dev.php' : 'app.php',
));
echo $request->getPathInfo();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM