Home > Article > Backend Development > Summary of usage of YII path, summary of usage of YII path_PHP tutorial
In Yii, if it is //, protected/views/layouts will be adjusted by default, // represents the absolute path. This is actually the relationship between absolute and relative / represents a relative path, such as layout under module/user. If a single slash is used, the view under the currently activated module will be searched first by default. If there is no currently activated module, the search will be started from the system root directory. If a double slash is used, the view will be searched directly from the system root
Namespace constants defined by Yii framework:
system: points to the Yii framework directory; YIIframework
zii: points to the zii library directory; YIIframeworkzii
application: points to the application base directory; protected
webroot: Points to the directory containing the entry script file. This alias is effective since version 1.0.3.
ext: points to the directory containing all third-party extensions, available since version 1.0.8; protectedextensions
Yii::getPathOfAlias('zii') Yii::import ('zii.*') Yii::setPathOfAlias('backend', $backend); 'import' => array( 'backend.models.*',
The home directory of the application refers to the root directory that contains all PHP code and data with a relatively high security factor. By default, this directory is generally a directory in the directory where the entry code is located: protected. This path can be changed by setting basePath in the 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 explain in protected/config/main.php:
'import'=>array( ...... 'application.venders.*', ),
Insert meta information:
Yii::app()->clientScript->registerMetaTag('keywords','关键字'); Yii::app()->clientScript->registerMetaTag('description','一些描述'); Yii::app()->clientScript->registerMetaTag('author','作者'); <link rel="alternate" type="application/rss+xml" href="http://www.bkjia.com/" /> Yii::app()->clientScript->registerLinkTag('alternate','application/rss+xml',$this->createUrl('/feed'));
Add CSS files or JavaScript files in 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 the registerCoreScript key can be viewed in the framework/web/js/packages.php list:
Yii::app()->clientScript->registerCoreScript('jquery');
Get the ID of the current controller in the view:
Yii::app()->getController()->id;
Get the ID of the current action in the view:
Yii::app()->getController()->getAction()->id;
yii gets ip address
Yii::app()->request->userHostAddress;
yii determines the submission method
Yii::app()->request->isPostRequest
Get the current domain name:
Yii::app()->request->hostInfo
Get the physical path of the protected directory
YII::app()->basePath;
Get the url of the previous page to return
Yii::app()->request->urlReferrer;
Get the current url
Yii::app()->request->url;
Get current home url
Yii::app()->homeUrl
Get the current return url
Yii::app()->user->returnUrl
Project path
dirname(Yii::app()->BasePath)
If you have a directory with some commonly used classes or files, you can define a path alias at the top of main.php, and the alias can be translated into its corresponding path.
Yii::getPathOfAlias('webroot')
If there are multiple, you can add a configuration in 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
There is no problem in writing this method, but if it is a multi-table joint query, it is not recommended to write it in the search method. When Yii automatically generates a model, it usually generates a search method, and the search method is generally used as a query for the model. That is, a separate table query.
If you want to query multiple tables together, it would be great to write another method! Moreover, the query method can be written in the action. There is no need to write the multi-table query in the model
Haha. The above is just a personal habit and is for reference only.
The query results returned by CActiveDataProvider usually obtain a list array through the getData method. There are many examples of yii on the Internet. You can understand it by just checking it
Controller, model and view are inherently independent. You can just apply the view template you want. As long as you use Yii's CActiveForm widget in the view form, the verification function can be realized