Opencart元标题包括商店名称 [英] Opencart meta title include store name
问题描述
在Document类中时如何获取商店名称.这就是我想要做的:
How to get the store name when in the Document class. This is what I am trying to do:
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
指的是文档类.如何获取配置?
Although the $this
is referring to the document class. How to I get the config?
使用最新版本的opencart 1.5.2.1
Using the latest version of opencart 1.5.2.1
当您检查index.php
文件以查看配置的加载方式
When you check the index.php
file to see how config is loaded
// Registry
$registry = new Registry();
// Loader
$loader = new Loader($registry);
$registry->set('load', $loader);
// Config
$config = new Config();
$registry->set('config', $config);
推荐答案
Opencart使用某种依赖注入来从库类访问注册表.此技术已应用于许多图书馆类别,例如客户,会员,货币,税项,重量,长度和购物车类别.令人惊讶的是,文档类是为数不多的未传递注册表对象的类之一.
Opencart uses some kind of dependency injection to access the registry from library classes. This technique is applied in many library classes, like the customer, affiliate, currency, tax, weight, length and cart class. Surprisingly, the document class is one of the few classes that don't get the registry object passed in.
如果您想遵循此约定,建议您修改index.php和library/document.php,以便Document构造函数将注册表作为参数:
If you'd like to follow this convention, I'd suggest you modify index.php and library/document.php so that the Document constructor takes the registry as an argument:
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中的Document类中,如下所示:
Now you only need to inject the registry object into the Document class in index.php, as follows:
// Registry
$registry = new Registry();
[...]
// Document
$registry->set('document', new Document($registry));
这篇关于Opencart元标题包括商店名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!