2014-05-06 18 views
0

我Yii的模塊文件結構看起來像這樣警予如何鏈接到模塊圖像

-yii 
    -protected 
    -modules 
     -admin 
      -controller 
      -model 
      -view 
       -layout 
        -main.php 
      -assets 
       -css 
        -style.css 
       -js 
       -images 
        -logo.jpg 

我想要做的就是從我protected/views/site/index.php訪問logo.jpg。我怎麼做?

回答

2

你應該首先發布你的資產,然後使用這個URL。你的模塊中添加以下代碼:

class YourModule extends CWebModule 
{ 
    private $_assetsUrl; 

    public function getAssetsUrl() 
    { 
     if ($this->_assetsUrl === null) 
      $this->_assetsUrl = Yii::app()->getAssetManager()->publish(__DIR__ . '/assets', false, -1, true); 
     return $this->_assetsUrl; 
    } 

然後使用這個URL:

$assets = $this->module->getAssetsUrl(); 
echo $assets . '/images/logo.jpg' 
+0

我得到此錯誤' user2636556

+0

$ assets = $ this-> module-> getAssetsUrl(); – Alex

+0

不是'$ this-> module-> getAssetsUrl();'如果您是從同一個模塊內調用,則使用? – user2636556