I want to get the various paths to the web directory in action
Reply content:
I want to get the various paths to the web directory in action
For your needs, SYMFONY2 provides kernel services through DIC, as well as a request package.
In the controller (in other places you can inject kernel yourself, this service is a class in the Httpkernel library: Good kids like to read the source code):
$appRoot = $this->get('kernel')->getRootDir(); // 这里得到的是app目录的绝对路径// ... 然后你可以再定位到web目录了
The relative path to the Web directory? What's the relative path? No matter what your actual needs are, SF2 provides you with a ready-made tool that you can compare to to get a relative path:
use Symfony\Component\Filesystem\Filesystem;$filesystem = new Filesystem();$filesystem->makePathRelative($endPath, $startPath); // $endPath相对$startPath的相对路径
As for the URL, you can get it by request:
// 在controll里:$this->getRequest()->getHost(); // 主机名$this->getRequest()->getHttpHost(); // 带协议的主机名$this->getRequest()->getRequestUri(); // 请求的路径
There are a lot of ways to see it yourself.