我正在扩展时事通讯区域,以便我可以发送包含产品列表的电子邮件,但我无法找到如何获取每个产品的 seo url?
当我这样做时,我实际上得到了管理 URL。
$this->url->link('product/product', 'product_id=' . $row['product_id'])
店面中的相同代码工作正常,如果存在 seo url 则返回。
我可以让它工作的唯一方法是如果我手动构建 URL,显然没有 seo url。
HTTP_CATALOG .'index.php?route=product/product&product_id='.$row['product_id']
进一步调查,我发现管理区域缺少以下代码,但我无法弄清楚它实际上是如何工作的并与 $this->url->link
相关联,因此我可以修改它以使其适合我。
$controller->addPreAction(new Action('common/seo_url'));
更新 - 最后最简单的解决方案是添加我自己的方法,如下所示:
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;
}
最佳答案
前端链接
在不修改核心文件的情况下从管理区域创建前端链接的最佳方法是创建您自己的 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 网址
获取 SEO URL 有点困难,不幸的是需要复制一些代码或一些麻烦。
复制的代码
基本上,从 catalog/controller/common/seo_url.php
复制整个 rewrite($link)
函数,并将其添加到您正在创建的 Controller 类中.然后在上面提到的新 $url
行之后添加以下行:
$url->addRewrite($this);
然后将当前 Controller 用作重写器,重写所有 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] ...
}
}
您将从管理区域获得 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']);
关于php - opencart 从管理员中获取产品 seo url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20245791/