2012-04-10 80 views
0

如何在Document類中獲取商店名稱。這是我想要做的:Opencart元標題包含商店名稱

public function setTitle($title) { 

    // Append store name if small title 
    if(strlen($title) < 30){ 
     $this->title = $title . ' - ' . $this->config->get("store_name"); 
    } else { 
     $this->title = $title; 
    } 
} 

儘管$this指的是文檔類。如何獲得配置?

使用最新版本1.5.2.1 Opencart的

的當您檢查index.php文件,以便了解

// Registry 
$registry = new Registry(); 

// Loader 
$loader = new Loader($registry); 
$registry->set('load', $loader); 

// Config 
$config = new Config(); 
$registry->set('config', $config); 
+0

檢入文件...某處你可以找到這個:'$ this-> config = New' ...你會看到使用的類。 – 2012-04-16 15:30:30

+0

我已經添加了這部分來展示我想要實現的目標。 '$ this-> config'不在該類中。 – 2012-04-16 17:25:00

回答

4

Opencart使用某種依賴注入從庫類訪問註冊表。這項技術應用於許多圖書館類,如客戶,附屬公司,貨幣,稅收,體重,長度和購物車類。令人驚訝的是,文檔類是少數幾個不通過註冊表對象的類之一。

如果你想遵循這個約定,我建議你修改index.php和庫/文檔。 PHP使得文檔構造函數將註冊表參數:

class Document { 

     [...] 

     // Add the constructor below 
     public function __construct($registry) { 
       $this->config = $registry->get('config'); 
     } 

     [...] 

     public setTitle($title) { 
      if(strlen($title) < 30){ 
       $this->title = $title . ' - ' . $this->config->get("store_name"); 
      } else { 
       $this->title = $title; 
      } 
     } 

} 

現在你只需要注入註冊表對象到index.php的文檔類,如下所示:

// Registry 
$registry = new Registry(); 

[...] 

// Document 
$registry->set('document', new Document($registry)); 
+0

完美我在等待一個答案,它提供了一種通過只編輯幾個文件來完成此任務的方法。 – 2012-04-19 12:05:59

1

不能使用配置加載$這個 - > cofig文檔類裏面,因爲它沒有配置屬性,它也沒有魔術__get方法,像控制器類。

你可以嘗試改變你的頭控制器。

public function index() { 

    $title = $this->document->getTitle(); 
    if(strlen($title) < 30){ 
     $this->data['title'] = $title . ' - ' . $this->config->get("store_name"); 
    } else { 
     $this->data['title'] = $title; 
    } 

    // .... 
} 

-------- --------修訂

如果你想使用的文檔類的內部配置$,你可以使用全局變量:

public function setTitle($title) { 

    global $config; 
    // Append store name if small title 
    if(strlen($title) < 30){ 
     $this->title = $title . ' - ' . $config->get("store_name"); 
    } else { 
     $this->title = $title; 
    } 
} 

但我建議你不要這樣做。

1

在Opencart的1.5.1.3工作將$this->config->get("store_name")更改爲$this->config->get("config_name")

+1

適用於1.5.5.1 – 2013-05-14 20:05:42

相關問題