Symfony2 显式获取 URL 的路径信息 [英] Symfony2 get path info of URL explicitely

查看:14
本文介绍了Symfony2 显式获取 URL 的路径信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比如说,我有一个 URL http://server/mysite/web/app_dev.php/resource/1.我正在执行一个 GET 请求,相应的操作是 ResourceController::getAction.

Say, I have an URL http://server/mysite/web/app_dev.php/resource/1. I am doing a GET request and the corresponding action is ResourceController::getAction.

在这个控制器动作中,如果我调用 $request->getPathInfo(),它会给我 /resource/1.

In this controller action if I call $request->getPathInfo(), it gives me /resource/1.

但是在同一个控制器中,如果我使用另一个资源的 url 创建一个 Request 对象并调用 getPathInfo() 它返回一个更长的版本.

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.

$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

在这种情况下,如何让 getPathInfo() 只返回 /another_resource/1 ?

How is it possible to make getPathInfo() to return only /another_resource/1 in this case?

任何人都可以建议将端点 URL http://server/mysite/web/app_dev.php/another_resource/1 转换为 /another_resource/1 的最安全方法是什么代码>在Symfony2中?

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?

控制器操作正在接收请求内容中的一些 URL.该操作需要解析这些 URL 以识别相应的资源.我正在尝试使用 $router->match 函数从 URL 中检索参数.匹配函数只需要 /another_resource/1 部分.

The controller action is receiving some URLs in request content. The action needs to parse those URLs to recognize the corresponding resource. I am trying to make use to $router->match function to retrieve the parameters from the URL. The match function expects only /another_resource/1 part.

推荐答案

Request::create 方法创建一个不知道当前请求的任何信息的新请求,它返回完整的 uri,因为它不知道当前的脚本文件.试试:

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. 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();

这篇关于Symfony2 显式获取 URL 的路径信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆