magento - 如何在magento中编辑标签url?

标签 magento url tags

我想在 magento 中制作 SEO 友好的标签 URL。

目前是abc.com/tag/product/list/tagId/17/
但我想让它 abc.com/tag/xyz

我尝试使用“URL 重写管理”,但它不起作用。

请帮忙。

最佳答案

首先我想说这是一个很好的问题。让我兴奋不已。
它可以与 url 管理一起使用,但有点麻烦。工作量很大。
例如我在url管理中添加了这个。

Type : Custom
Store: Select any store here - if you have more you have to do this process for each one
ID Path: TAG_23
Request Path: tag/camera
Target Path: tag/product/list/tagId/23
Redirect: No

已保存。现在,当调用 ROOT/tag/camera 时,我看到标有“camera”的产品。
但可以肯定的是,这不是正确的出路。如果你有超过 10 个标签,你就会感到无聊。

所以我们的想法是制作一个模块,使 magento 能够识别 tag/something 之类的标签,并将标签的链接更改为与上面相同的格式,这样你就不必编辑很多模板。
我将该模块命名为 Easylife_Tag。您需要以下文件。

app/etc/modules/Easylife_Tag.xml - 声明文件

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Tag>
            <codePool>local</codePool>
            <active>true</active>
            <depends>
                <Mage_Tag />
            </depends>
        </Easylife_Tag>
    </modules>
</config>

app/code/local/Easylife/Tag/etc/config.xml - 配置文件

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Tag>
            <version>1.0.0</version>
        </Easylife_Tag>
    </modules>
    <global>
        <events>
            <controller_front_init_routers><!-- add a custom router to recognize urls like tag/something -->
                <observers>
                    <easylife_tag>
                        <class>Easylife_Tag_Controller_Router</class>
                        <method>initControllerRouters</method>
                    </easylife_tag>
                </observers>
            </controller_front_init_routers>
        </events>
        <models>
            <tag>
                <rewrite>
                    <tag>Easylife_Tag_Model_Tag</tag><!-- rewrite the tag model to change the url of the tags to tag/something -->
                </rewrite>
            </tag>
            <tag_resource>
                <rewrite>
                    <tag>Easylife_Tag_Model_Resource_Tag</tag> <!-- rewrite the tag resource model - see below why is needed -->
                </rewrite>
            </tag_resource>
        </models>
    </global>
</config>

app/code/local/Easylife/Tag/Model/Tag.php - 重写的标签模型

<?php
class Easylife_Tag_Model_Tag extends Mage_Tag_Model_Tag {
    //change the url from `tag/product/list/tagId/23` to `tag/camera`
    public function getTaggedProductsUrl() {
        return Mage::getUrl('', array('_direct' => 'tag/'.$this->getName()));
    }
}

app/code/local/Easylife/Tag/Model/Resource/Tag.php - 重写标签资源模型

<?php
class Easylife_Tag_Model_Resource_Tag extends Mage_Tag_Model_Resource_Tag {
    //by default, when loading a tag by name magento does not load the store ids it is allowed in
    //this method loads also the store ids
    public function loadByName($model, $name){
        parent::loadByName($model, $name);
        if ($model->getId()) {
            $this->_afterLoad($model);
        }
        else {
            return false;
        }
    }
}

app/code/local/Easylife/Tag/Controller/Router.php - 自定义路由器 - 请参阅内联注释

<?php
class Easylife_Tag_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract{
    public function initControllerRouters($observer){
        $front = $observer->getEvent()->getFront();
        $front->addRouter('easylife_tag', $this);
        return $this;
    }
    public function match(Zend_Controller_Request_Http $request){
        //if magento is not installed redirect to install
        if (!Mage::isInstalled()) {
            Mage::app()->getFrontController()->getResponse()
                ->setRedirect(Mage::getUrl('install'))
                ->sendResponse();
            exit;
        }
        //get the url key
        $urlKey = trim($request->getPathInfo(), '/');
        //explode by slash
        $parts = explode('/', $urlKey);
        //if there are not 2 parts (tag/something) in the url we don't care about it.
        //return false and let the rest of the application take care of the url.
        if (count($parts) != 2) {
            return false;
        }
        //if the first part of the url key is not 'tag' we don't care about it
        //return false and let the rest of the application take care of the url
        if ($parts[0] != 'tag') {
            return false;
        }
        $tagName = $parts[1]; //tag name
        //load the tag model
        $tag = Mage::getModel('tag/tag')->loadByName($tagName);
        //if there is no tag with this name available in the current store just do nothing
        if(!$tag->getId() || !$tag->isAvailableInStore()) {
            return false;
        }
        //but if the tag is valid
        //say to magento that the request should be mapped to `tag/product/list/tagId/ID_HERE` - the original url
        $request->setModuleName('tag')
            ->setControllerName('product')
            ->setActionName('list')
            ->setParam('tagId', $tag->getId());
        $request->setAlias(
            Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
            $urlKey
        );
        return true;
    }
}

就是这样。清除缓存并尝试一下。

[编辑]
You can find the full extension here 。唯一的区别是它使用社区代码池而不是如上所述的本地代码池。

关于magento - 如何在magento中编辑标签url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24608684/

相关文章:

javascript - 关闭 <脚本>

algorithm - 计算趋势主题或标签的最佳方法是什么?

php - Magento 1.4 $_POST ['field_name'] 不工作

magento - 当具有 local.xml 的自定义主题处于事件状态时,包的 local.xml 未加载

jquery - 我可以在同一个文件中包含 jQuery 和 AJAX 吗?这会冲突吗?

javascript - 如何阻止 img src 在脚本中使用当前 URL 和 URL

c# - Regex 如何获取 url() 的内容

ios - 通过标签动态查找 iOS UIView

Magento 购物车 SSL 问题将数据提交到不安全的位置

url - 指定 rest.Resource 中的内容类型(rest.go lib & Golang)