Symfony2 what is the function of getting the absolute path, relative path, URL of the web directory

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.