轉載地址:http://hudeyong926.iteye.com/blog/1283125
如果是 // 就會默認去調 protected/views/layouts,//代表 絕對路徑
其實 就是 絕對和相對的關係 /代表相對路徑,如module/user下的layout
用單斜槓的話默認會先找當前已經激活的模塊底下的view,若當前未有激活的模塊則從系統根目錄下開始找,雙斜槓的話就直接從系統根下開始找
Java代碼 收藏代碼
$this->renderPartial('application.modules.admin.views.product._search')
Yii framework已經定義的命名空間常量
system: 指向Yii框架目錄; YII\framework
zii: 指向zii library 目錄; YII\framework\zii
application : 指向應用程序基本目錄; protected\
webroot: 指向包含裏入口腳本 文件的目錄. 此別名自 1.0.3 版起生效. \
ext : 指向包含所有第三方擴展的目錄, 從版本 1.0.8 可用; \protected\extensions
Java代碼 收藏代碼
Yii::getPathOfAlias('zii')
Yii::import ('zii.*')
Yii::setPathOfAlias('backend', $backend);
'import' => array(
'backend.models.*',
應用的主目錄是指包含所有安全係數比較高的PHP代碼和數據的根目錄。在默認情況下,這個目錄一般是入口代碼所在目錄的一個目錄: protected 。這個路徑可以通過在application configuration裏設置 basePath來改變.
YII framework路徑
Java代碼 收藏代碼
Yii::getFrameworkPath()
YII 文件下載
Java代碼 收藏代碼
Yii::app()->getRequest()->sendFile('test.txt', 'File content goes here.');
protected/runtime
Java代碼 收藏代碼
Yii::app()->getRuntimePath();
protected/venders目錄
Java代碼 收藏代碼
Yii::import('application.venders.*');
或在protected/config/main.php說明
Java代碼 收藏代碼
'import'=>array(
......
'application.venders.*',
),
插入meta信息
Java代碼 收藏代碼
Yii::app()->clientScript->registerMetaTag('關鍵字','keywords');
Yii::app()->clientScript->registerMetaTag('一些描述','description');
Yii::app()->clientScript->registerMetaTag('作者','author');
Yii::app()->clientScript->registerMetaTag(' text/html;charset=utf-8', null, 'Content-Type');
<link rel="alternate" type="application/rss+xml" href="http://www.dreamdu.com/feed/" />
Java代碼 收藏代碼
Yii::app()->clientScript->registerLinkTag('alternate','application/rss+xml',$this->createUrl('/feed'));
如何在控制器添加CSS文件或JavaScript文件
Java代碼 收藏代碼
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列表中可以查看
Java代碼 收藏代碼
Yii::app()->clientScript->registerCoreScript('jquery');
在view中得到當前controller的ID方法:
Java代碼 收藏代碼
Yii::app()->getController()->id;
在view中得到當前action的ID方法:
Java代碼 收藏代碼
Yii::app()->getController()->getAction()->id;
yii獲取ip地址
Java代碼 收藏代碼
Yii::app()->request->userHostAddress;
yii判斷提交方式
Java代碼 收藏代碼
Yii::app()->request->isPostRequest isDeleteRequest isAjaxRequest
得到當前域名:
Java代碼 收藏代碼
Yii::app()->request->hostInfo
得到proteced目錄的物理路徑
Java代碼 收藏代碼
YII::app()->basePath;
得到模塊名稱
Java代碼 收藏代碼
$module = Yii::app()->getModule()->getId();
獲得上一頁的url以返回
Java代碼 收藏代碼
Yii::app()->request->urlReferrer;
得到當前url
Java代碼 收藏代碼
Yii::app()->request->url;
得到當前home url
Java代碼 收藏代碼
Yii::app()->homeUrl
得到當前return url
Java代碼 收藏代碼
Yii::app()->user->returnUrl
項目路徑
Java代碼 收藏代碼
dirname(Yii::app()->BasePath)
Yii::app()->getBaseUrl(true);
<?php echo $this->getLayoutFile('main'); ?>
$this->redirect('index.php?r=admin/manage');
{createUrl()}
Java代碼 收藏代碼
echo $this->createUrl('urlBoyLeeTest');
//out => /yii_lab/index.php?r=lab/urlBoyLeeTest
$this->createUrl('post/read') // /index.php/post/read
Java代碼 收藏代碼
<?php echo Yii::app()->request->baseUrl; ?>/css/screen.css
Yii::app()->theme->baseUrl.'/images/FileName.gif'
{createAbsoluteUrl()}
Java代碼 收藏代碼
echo $this->createAbsoluteUrl('urlBoyLeeTest');
//out => http://localhost/yii_lab/index.php?r=lab/urlBoyLeeTest