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.