Home > PHP Framework > YII > body text

How to get the current url and domain name in yii

王林
Release: 2020-09-28 17:20:42
forward
2889 people have browsed it

How to get the current url and domain name in yii

Assume that the access address of our current page is:

http://localhost/CMS/public/index.php?r=news&id=1
Copy after login

(Recommended tutorial: yii)

1,

1. Get the current domain name:

echo Yii::app()->request->hostInfo;
Copy after login

Result:

http://localhost
Copy after login

2. Get the current URL (except domain name):

echo Yii::app()->request->getUrl();
Copy after login

Result:

/CMS/public/index.php?r=news&id=1
Copy after login

3. Get the homepage address (except the domain name):

echo Yii::app()->user->returnUrl;
Copy after login

Result:

/CMS/public/index.php
Copy after login
Copy after login

4. Get the root directory address (except the domain name):

echo Yii::app()->homeUrl;
Copy after login

Result:

/CMS/public/index.php
Copy after login
Copy after login

5,

echo Yii::app()->request->baseUrl;
Copy after login

Result:

/CMS/public
Copy after login
Copy after login

6. Get the relative address of the website root directory:

echo Yii::app()->baseUrl;
Copy after login

Result:

/CMS/public
Copy after login
Copy after login

(Note: If you directly use this method to point to the root directory of the website, it is best to add a "/" after it, because sometimes if you do not add "/", it may not point to the root directory of the website correctly. Application examples : Home )
2.
7. YII obtains the IP address :

Yii::app()->request->userHostAddress;
Copy after login

8. Yii determines the submission method:

Yii::app()->request-isPostRequest;
Copy after login

9. The physical path of the protected directory:

Yii::app()->basePath;
Copy after login

10. Get the url of the previous page to return:

Yii::app()->request->urlReferrer;
Copy after login

11. Get the current controller ID:

Yii::app()->getController()->getAction()->id;
Copy after login

12. Project path:

dirname(Yii::app()->BasePath);
Copy after login

13. Yii gets the data from get and post:

Yii::app()->request->getParam('id');
Copy after login

3.

14. How to set the time zone in Yii:

You can set the time zone in 'timeZone'=>'Asia/Chongqing' in config/main.php

15. Prevent duplicate submissions:

Ccontroler->refresh();
Copy after login

The above is the detailed content of How to get the current url and domain name in yii. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!