Symfony2 the method of obtaining the absolute path, relative path and web address of web directory _php instance

Source: Internet
Author: User
Tags codeigniter zend framework

This article describes the method of obtaining the absolute path, relative path and web address of Symfony2. Share to everyone for your reference, specific as follows:

For your needs, SYMFONY2 provides kernel services through DIC, as well as the encapsulation of request (requests).

In controller (where you can inject yourself into kernel, this service is a class in the Httpkernel library: Good kids like to read the source code):

$appRoot = $this->get (' kernel ')->getrootdir (); Here is the absolute path to the app directory
//... And then you can navigate to the Web directory again.

The relative path of the Web directory? Relative to what 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); Relative path of $endPath relative $startpath

As for the Web site, you can get it by request:

In controll:
$this->getrequest ()->gethost ();//host name
$this->getrequest ()->gethttphost ();// Host name with protocol
$this->getrequest ()->getrequesturi ();//requested Path

There are many more methods, you can click here to download the site to view.

More interested in Symfony2 related content readers can view the site topics: "Symfony Framework Introductory Course", "CodeIgniter Introductory Course", "CI (CodeIgniter) Framework Advanced Course", "PHP Excellent Development Framework Summary", " thinkphp Getting Started tutorial, thinkphp Common methods Summary, Zend Framework Framework Introduction Tutorial, PHP object-oriented Program Introduction tutorial, Php+mysql database operation Getting Started tutorial and PHP Common database operation Skills Summary

I hope this article will help you with the PHP program design based on SYMFONY2 framework.

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.