Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在magento中编辑标记url?_Magento_Url_Tags - Fatal编程技术网

如何在magento中编辑标记url?

如何在magento中编辑标记url?,magento,url,tags,Magento,Url,Tags,我想使搜索引擎优化友好的标记在magento网址 目前是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 h

我想使搜索引擎优化友好的标记在magento网址

目前是
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\u标签
。您需要以下文件

app/etc/modules/Easylife\u Tag.xml
-声明文件

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Tag>
            <codePool>local</codePool>
            <active>true</active>
            <depends>
                <Mage_Tag />
            </depends>
        </Easylife_Tag>
    </modules>
</config>
<?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/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;
    }
}

我使用Magento 1.8.1并尝试了马吕斯的解决方案,但我遇到了一个问题:2+个关键字标记(单词之间有空格)将转到404页,url中的空格更改为%20。一个关键词标签就像一个符咒

因此,我修改了他的模块,在标记模块上显示一个空格单词,并在URL中显示“连字符”

文件:Easylife/Tag/Model/Tag.php

return Mage::getUrl('', array('_direct' => 'tag/'.str_replace(" ", "-", $this->getName())));
文件:Easylife/Tag/Controller/Router.php

$tagName = str_replace("-", " ", $parts[1]); //tag name
它现在对我有用


向模块Marius致以最良好的问候和感谢

马吕斯,你是马根托God@DavidWilkins别说了。你让我脸红了你到底是如何找到
setAlias(…
部分的?Magento没有任何自动测试,你是否有一套专门的手动测试来捕捉这类东西?老实说,我不知道如何在Magento中编写安全的代码,而这一次,让我大吃一惊。
$tagName = str_replace("-", " ", $parts[1]); //tag name