Home > Backend Development > PHP Tutorial > YII路径的用法总结_PHP

YII路径的用法总结_PHP

WBOY
Release: 2016-06-01 11:52:00
Original
990 people have browsed it

在yii中如果是 // 就会默认去调 protected/views/layouts,//代表绝对路径。这其实就是绝对和相对的关系 /代表相对路径,如module/user下的layout。使用单斜杠的话默认会先找当前已经激活的模块底下的view,若当前未有激活的模块则从系统根目录下开始找,双斜杠的话就直接从系统根下开始找

Yii framework已经定义的命名空间常量:

system: 指向Yii框架目录; YII\framework
zii: 指向zii library 目录; YII\framework\zii
application: 指向应用程序基本目录;  protected\
webroot: 指向包含里入口脚本文件的目录. 此别名自 1.0.3 版起生效. \
ext: 指向包含所有第三方扩展的目录, 从版本 1.0.8 可用;  \protected\extensions

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

Copy after login

应用的主目录是指包含所有安全系数比较高的PHP代码和数据的根目录。在默认情况下,这个目录一般是入口代码所在目录的一个目录: protected。这个路径可以通过在application configuration里设置 basePath来改变.

YII framework路径:

Yii::getFrameworkPath() 
{full URL}

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

Copy after login

protected/venders目录:

Yii::import('application.venders.*');  
Copy after login

或在protected/config/main.php说明:

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

Copy after login

插入meta信息:

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.bitsCN.com/" />
Yii::app()->clientScript->registerLinkTag('alternate','application/rss+xml',$this->createUrl('/feed')); 

Copy after login

在控制器添加CSS文件或JavaScript文件:

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

Copy after login

调用YII框架中framework/web/js/source的js,其中registerCoreScript key调用的文件在framework/web/js/packages.php列表中可以查看:

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

Copy after login

在view中得到当前controller的ID方法:

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

Copy after login

在view中得到当前action的ID方法:

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

Copy after login

yii获取ip地址

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

Copy after login

yii判断提交方式

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

Copy after login

得到当前域名:

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

Copy after login

得到proteced目录的物理路径

YII::app()->basePath; 

Copy after login

获得上一页的url以返回

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

Copy after login

得到当前url

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

Copy after login

得到当前home url

Yii::app()->homeUrl 

Copy after login

得到当前return url

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

Copy after login

项目路径

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

Copy after login

如果你自己有个目录下有些类或文件常用,可以在main.php的最上边定义一个路径别名,别名可以被翻译为其相应的路径。

Yii::getPathOfAlias('webroot')  

Copy after login

如果是多个可以在main.php中的array中加一个配置

'aliases'=>array( 
'local'=>'path/to/local/' 
), 
<&#63;php echo $this->getLayoutFile('main'); &#63;>
$this->redirect('index.php&#63;r=admin/manage');
{createUrl()}
echo $this->createUrl('urlBoyLeeTest'); 
//out => /yii_lab/index.php&#63;r=lab/urlBoyLeeTest 
$this->createUrl('post/read') // /index.php/post/read 
<&#63;php echo Yii::app()->request->baseUrl; &#63;>/css/screen.css 
Yii::app()->theme->baseUrl.'/images/FileName.gif'  
{createAbsoluteUrl()}
echo $this->createAbsoluteUrl('urlBoyLeeTest'); 
//out => http://localhost/yii_lab/index.php&#63;r=lab/urlBoyLeeTest 
Copy after login
Related labels:
source:php.cn
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