symfony2取得web目录绝对路径、相对路径、网址的函数是什么

WBOY
Release: 2016-06-06 20:49:17
Original
933 people have browsed it

我想在Action中取得web目录的各种路径

回复内容:

我想在Action中取得web目录的各种路径

对于你的需求,Symfony2通过DIC提供了kernel服务,以及request(请求)的封装。

在controller里(在其他地方你可以自行注入kernel,这个服务是HttpKernel库里的一个类:好孩子都喜欢读源代码):

<code>$appRoot = $this->get('kernel')->getRootDir(); // 这里得到的是app目录的绝对路径
// ... 然后你可以再定位到web目录了
</code>
Copy after login

web目录的相对路径?相对什么的路径?不管你的实际需求是什么,sf2给你提供了现成的工具,你可以比对获取相对路径:

<code>use Symfony\Component\Filesystem\Filesystem;
$filesystem = new Filesystem();
$filesystem->makePathRelative($endPath, $startPath); // $endPath相对$startPath的相对路径
</code>
Copy after login

至于网址,你可以通过request获得:

<code>// 在controll里:
$this->getRequest()->getHost(); // 主机名
$this->getRequest()->getHttpHost(); // 带协议的主机名
$this->getRequest()->getRequestUri(); // 请求的路径
</code>
Copy after login

还有很多很多的方法,请自己查看

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!