Summary of usage of YII path
- 2021-07-07 06:43:44
- OfStack
In yii, if it is//, protected/views/layouts will be debugged by default, and//represents the absolute path. This is actually the absolute and relative relationship/represents the relative path, such as layout under module/user. If you use a single slash, you will first find view under the currently activated module by default. If there is no currently activated module, you will start to find it from the root directory of the system. If you use a double slash, you will start to find it directly from the root of the system
Yii framework already defined namespace constants:
system: Point to the Yii Framework Directory; YII\ framework
zii: Point to the zii library directory; YII\ framework\ zii
application: Points to the application base directory; protected\
webroot: Points to the directory that contains the entry script files. This alias has been in effect since version 1.0. 3.\
ext: Points to the directory containing 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 application home directory refers to the root directory containing all PHP codes and data with high safety factor. By default, this directory 1 is one of the directories where the entry code resides: 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->registerMetaTag('keywords',' Keyword ');
Yii::app()->clientScript->registerMetaTag('description','1 Some descriptions ');
Yii::app()->clientScript->registerMetaTag('author',' Author ');
<link rel="alternate" type="application/rss+xml" href="https://www.ofstack.com/" />
Yii::app()->clientScript->registerLinkTag('alternate','application/rss+xml',$this->createUrl('/feed'));
Add an CSS file or an 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 js of framework/web/js/source in the YII framework, where the file called by registerCoreScript key can be viewed in the list of framework/web/js/packages. php:
Yii::app()->clientScript->registerCoreScript('jquery');
The ID method for obtaining the current controller in view:
Yii::app()->getController()->id;
The ID method for obtaining the current action in view:
Yii::app()->getController()->getAction()->id;
yii Get ip Address
Yii::getFrameworkPath()
{full URL}
0
yii Determines Submission Mode
Yii::getFrameworkPath()
{full URL}
1
Get the current domain name:
Yii::getFrameworkPath()
{full URL}
2
Get the physical path of the proteced directory
YII::app()->basePath;
Get the url from the previous page to return
Yii::getFrameworkPath()
{full URL}
4
Get the current url
Yii::getFrameworkPath()
{full URL}
5
Get the current home url
Yii::getFrameworkPath()
{full URL}
6
Obtain the current return url
Yii::getFrameworkPath()
{full URL}
7
Project path
dirname(Yii::app()->BasePath)
If you have a directory in which some classes or files are commonly used, you can define a path alias at the top of main. php, and the alias can be translated into its corresponding path.
Yii::getFrameworkPath()
{full URL}
9
If it is multiple, you can add one configuration to 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