Home > Article > Backend Development > How to get the current url and domain name in Yii
The example in this article describes how Yii obtains the current url and domain name. The details are as follows:
Suppose the access address of our current page is: http://localhost/CMS/public/index.php?r=news&id=1
1. Get the current domain name:
The code is as follows:
echo Yii: :app()->request->hostInfo;
Result: http://localhost
2. Get the current URL (except the domain name):
The code is as follows:
echo Yii::app()-> ;request->getUrl();
Result: /CMS/public/index.php?r=news&id=1
3. Get the homepage address (except domain name):
The code is as follows:
echo Yii:: app()->user->returnUrl;
Result: /CMS/public/index.php
4. Get the root directory address (except the domain name):
The code is as follows:
echo Yii::app( )->homeUrl;
Result: /CMS/public/index.php
5.
The code is as follows:
echo Yii::app()->request->baseUrl;
Result: /CMS/ public
6. Get the relative address of the website root directory: echo Yii::app()->baseUrl;
Result: /CMS/public (Note: If you use this method to directly point to the website root directory, it is best to add it in other Add a "/" after it, because sometimes if you don't add "/", it may not point to the website root directory correctly. Application example: Home )
2.
7. YII obtains the IP address:
The code is as follows:
Yii::app()->request->userHostAddress;
8. Yii Determine the submission method:
The code is as follows:
Yii::app()->request-isPostRequest;
9. The physical path of the protected directory:
The code is as follows:
Yii::app()->basePath;
10. Get the url of the previous page to return:
The code is as follows:
Yii::app()->request->urlReferrer;
11. Get the current controller ID:
The code is as follows:
Yii: :app()->getController()->getAction()->id;
12. Project path:
The code is as follows:
dirname(Yii::app()->BasePath);
13. Yii gets the data from get and post:
The code is as follows:
Yii::app()->request->getParam(id);
3.
14. How to set the time zone in Yii:
Can be found in config/ timeZone=>Asia/Chongqing in main.php, set the time zone
15. Prevent repeated submissions:
The code is as follows:
Ccontroler->refresh();
16. Get the url of the previous page and return it
Yii ::app()->request->urlReferrer