YII path usage summary _ php instance

Source: Internet
Author: User
This article mainly introduces the usage and summary of the YII path. If you need a friend, you can refer to the yii file. If it is //, protected/views/layouts will be called by default, // represents the absolute path. This is actually the absolute and relative relationship/Represents the relative path, such as layout under module/user. If a single slash is used, the view under the activated module is first searched by default. If no activated module is found, the view is located in the system root directory, if the double slash is used, you can directly find it from the system root.

The namespace constants defined by Yii framework:

System: Pointing to the Yii framework directory; YII \ framework
Zii: Pointing to the zii library directory; YII \ framework \ zii
Application: Point to the basic application directory; protected \
Webroot: Point to the directory containing the script file. This alias takes effect from version 1.0.3 .\
Ext: Points to a directory that contains all third-party extensions, available from version 1.0.8; \ protected \ extensions

Yii::getPathOfAlias('zii') Yii::import ('zii.*')  Yii::setPathOfAlias('backend', $backend); 'import' => array( 'backend.models.*',  

The main directory of an application is the root directory that contains all PHP code and data with a high security factor. By default, this directory is usually a directory of the entry code Directory: protected. This path can be changed by setting basePath in application configuration.

YII framework path:

Yii::getFrameworkPath() {full URL}

http://localhost/yii_lab/index.php?r=lab/urlBoyLeeTest 

Protected/venders directory:

Yii::import('application.venders.*');  

Or in protected/config/main. php:

'import'=>array(      ......      'application.venders.*',    ), 

Insert meta information:

Yii: app ()-> clientScript-> register1_ag ('keyword', 'keyword '); Yii: app ()-> clientScript-> register1_ag ('description ', 'Some description'); Yii: app ()-> clientScript-> register1_ag ('author', 'author ');
 Yii: app ()-> clientScript-> registerLinkTag ('alternet', 'application/rss + xml', $ this-> createUrl ('/feed '));

Add a CSS or JavaScript file to the Controller:

Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl.'/css/my.css'); Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/css/my.js'); <?php echo $this->module->assetsUrl; ?>/css/main.css 

Call the js of framework/web/js/source in the YII framework. The file called by registerCoreScript key can be viewed in the framework/web/js/packages. php list:

Yii::app()->clientScript->registerCoreScript('jquery'); 

Obtain the ID method of the current controller in view:

Yii::app()->getController()->id;  

Obtain the ID of the current action in the view:

Yii::app()->getController()->getAction()->id;  

Yii get IP Address

Yii::app()->request->userHostAddress; 

Yii judgment submission method

Yii::app()->request->isPostRequest  

Get the current domain name:

Yii::app()->request->hostInfo 

Obtain the physical path of the proteced directory.

YII::app()->basePath; 

Returns the url of the previous page.

Yii::app()->request->urlReferrer; 

Get the current url

Yii::app()->request->url; 

Get the current home url

Yii::app()->homeUrl 

Returns the current return url.

Yii::app()->user->returnUrl 

Project path

dirname(Yii::app()->BasePath) 

If you have a directory that is commonly used in some classes or files, you can define a path alias at the top of main. php. the alias can be translated into the corresponding path.

Yii::getPathOfAlias('webroot')  

You can add one or more configurations to the array in main. php.

'aliases'=>array( 'local'=>'path/to/local/' ), <?php echo $this->getLayoutFile('main'); ?>$this->redirect('index.php?r=admin/manage');{createUrl()}echo $this->createUrl('urlBoyLeeTest'); //out => /yii_lab/index.php?r=lab/urlBoyLeeTest $this->createUrl('post/read') // /index.php/post/read <?php echo Yii::app()->request->baseUrl; ?>/css/screen.css Yii::app()->theme->baseUrl.'/images/FileName.gif'  {createAbsoluteUrl()}echo $this->createAbsoluteUrl('urlBoyLeeTest'); //out => http://localhost/yii_lab/index.php?r=lab/urlBoyLeeTest 

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.