我正在扩展新闻通讯区域,以便我可以发送包含产品列表的电子邮件,但我无法找到如何获取每个产品的搜索引擎优化网址?
当我这样做时,我实际上最终得到了管理员URL.
$this->url->link('product/product', 'product_id=' . $row['product_id'])
Run Code Online (Sandbox Code Playgroud)
商店前端的相同代码工作正常,如果存在,则返回seo url.
我可以让它工作的唯一方法是,如果我手动构建URL,显然没有seo url.
HTTP_CATALOG .'index.php?route=product/product&product_id='.$row['product_id']
Run Code Online (Sandbox Code Playgroud)
进一步研究这个我看到管理区域缺少以下代码,但我无法弄清楚它是如何工作的并且与之相关联,$this->url->link以便我可以修改它以便为我工作.
$controller->addPreAction(new Action('common/seo_url'));
Run Code Online (Sandbox Code Playgroud)
更新 - 最后最简单的解决方案是添加我自己的方法,如下所示:
public function getUrl($route, $key, $value){
$url = "index.php?route={$route}&{$key}={$value}";
if($this->config->get('config_seo_url')){
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "'");
if($query->row['keyword']){
$url = $query->row['keyword'];
}
}
return $url;
}
Run Code Online (Sandbox Code Playgroud)
在不修改核心文件的情况下从管理区域创建前端链接的最佳方法是创建自己的URL对象:
$url = new Url(HTTP_CATALOG, $this->config->get('config_secure') ? HTTP_CATALOG : HTTPS_CATALOG);
要创建URL,然后使用$ url而不是$ this-> url,所以你的例子是:
$url->link('product/product', 'product_id=' . $row['product_id']);
获取SEO URL有点困难,不幸的是需要复制一些代码或一些躲避.
基本上,复制整个rewrite($link)函数catalog/controller/common/seo_url.php并将其添加到您正在创建的控制器类中.然后在$url上面提到的新行之后添加以下行:
$url->addRewrite($this);
然后将当前控制器用作重写器,并重写所有URL.您可能可以使用单独的类,但是有依赖项可以访问数据库.这似乎是最直接的方式,即使它是丑陋的.
那么你的代码应该是:
<?php
class ControllerMyController extends Controller {
public function index() {
...
$url = new Url(HTTP_CATALOG, $this->config->get('config_secure') ? HTTP_CATALOG : HTTPS_CATALOG);
if ($this->config->get('config_seo_url')) {
$url->addRewrite($this);
}
$url->link('product/product', 'product_id=' . $row['product_id']);
...
}
public function rewrite($link) {
... [stuff from seo_url.php] ...
}
}
Run Code Online (Sandbox Code Playgroud)
你会从管理区域获得SEO的URL.
如果您对任意require语句感到满意,那么您可以选择执行以下操作并使用现有的SEO代码(这意味着如果更改它将保持最新,但如果文件移动则会失败).
要以这种方式创建SEO URL,您的代码必须是:
$url = new Url(HTTP_CATALOG, $this->config->get('config_secure') ? HTTP_CATALOG : HTTPS_CATALOG);
if ($this->config->get('config_seo_url')) {
// Require the SEO file directly - path is relative to /admin/index.php
require_once('../catalog/controller/common/seo_url.php');
$rewriter = new ControllerCommonSeoUrl($this->registry);
$url->addRewrite($rewriter);
}
$url->link('product/product', 'product_id=' . $row['product_id']);
Run Code Online (Sandbox Code Playgroud)