溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何正確的使用YII路徑

發(fā)布時(shí)間:2021-01-11 15:20:15 來源:億速云 閱讀:176 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)如何正確的使用YII路徑,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

在yii中如果是 // 就會(huì)默認(rèn)去調(diào) protected/views/layouts,//代表絕對(duì)路徑。這其實(shí)就是絕對(duì)和相對(duì)的關(guān)系 /代表相對(duì)路徑,如module/user下的layout。使用單斜杠的話默認(rèn)會(huì)先找當(dāng)前已經(jīng)激活的模塊底下的view,若當(dāng)前未有激活的模塊則從系統(tǒng)根目錄下開始找,雙斜杠的話就直接從系統(tǒng)根下開始找

Yii framework已經(jīng)定義的命名空間常量:

system: 指向Yii框架目錄; YII\framework
zii: 指向zii library 目錄; YII\framework\zii
application: 指向應(yīng)用程序基本目錄;  protected\
webroot: 指向包含里入口腳本文件的目錄. 此別名自 1.0.3 版起生效. \
ext: 指向包含所有第三方擴(kuò)展的目錄, 從版本 1.0.8 可用;  \protected\extensions

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

應(yīng)用的主目錄是指包含所有安全系數(shù)比較高的PHP代碼和數(shù)據(jù)的根目錄。在默認(rèn)情況下,這個(gè)目錄一般是入口代碼所在目錄的一個(gè)目錄: protected。這個(gè)路徑可以通過在application configuration里設(shè)置 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','關(guān)鍵字'); 
Yii::app()->clientScript->registerMetaTag('description','一些描述'); 
Yii::app()->clientScript->registerMetaTag('author','作者'); 
<link rel="alternate" type="application/rss+xml" href="https://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

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

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

在view中得到當(dāng)前controller的ID方法:

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

在view中得到當(dāng)前action的ID方法:

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

yii獲取ip地址

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

yii判斷提交方式

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

得到當(dāng)前域名:

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

得到proteced目錄的物理路徑

YII::app()->basePath;

獲得上一頁的url以返回

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

得到當(dāng)前url

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

得到當(dāng)前home url

Yii::app()->homeUrl

得到當(dāng)前return url

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

項(xiàng)目路徑

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

如果你自己有個(gè)目錄下有些類或文件常用,可以在main.php的最上邊定義一個(gè)路徑別名,別名可以被翻譯為其相應(yīng)的路徑。

Yii::getPathOfAlias('webroot')

如果是多個(gè)可以在main.php中的array中加一個(gè)配置

'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

關(guān)于如何正確的使用YII路徑就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

yii
AI