当前位置:首页 > PHP教程 > PHP常见问题

YII路径的用法总结

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

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

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

yii::getpathofalias('zii') yii::import ('zii.*') yii::setpathofalias('backend', $backend); 'import' => array( 'backend.models.*',

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

yii framework路径:

yii::getframeworkpath() {full url}
http://localhost/yii_lab/index.php?r=lab/urlboyleetest

protected/venders目录:

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

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

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

插入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.jb51.net/" /> yii::app()->clientscript->registerlinktag('alternate','application/rss+xml',$this->createurl('/feed'));

在控制器添加css文件或javascript文件:

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

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

yii::app()->clientscript->registercorescript('jquery');

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

yii::app()->getcontroller()->id;

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

yii::app()->getcontroller()->getaction()->id;

yii获取ip地址

yii::app()->request->userhostaddress;

yii判断提交方式

yii::app()->request->ispostrequest

得到当前域名:

yii::app()->request->hostinfo

得到proteced目录的物理路径

yii::app()->basepath;

获得上一页的url以返回

yii::app()->request->urlreferrer;

得到当前url

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

得到当前home url

yii::app()->homeurl

得到当前return url

yii::app()->user->returnurl

项目路径

dirname(yii::app()->basepath)

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

yii::getpathofalias('webroot')

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

'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 


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!