2014-07-07 10 views
5

Ich möchte SEO freundliche Tag-URL in Magento machen.Wie zu bearbeiten Tag-URL in Magento?

Derzeit ist es abc.com/tag/product/list/tagId/17/
aber ich mag es abc.com/tag/xyz

ich dies machen versucht, durch „URL Rewrite-Management“ verwenden, aber es funktioniert nicht.

Bitte helfen.

Antwort

7

Zuerst möchte ich sagen, dass dies eine nette Frage ist. Hab mich alle angezündet.
Es funktioniert mit der URL-Verwaltung, aber es ist eine Art von Drag. Zu viel Arbeit.
Zum Beispiel habe ich dies in der URL-Verwaltung hinzugefügt.

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 

Gespeichert. jetzt wenn ich ROOT/tag/camera anrufe, sehe ich die Produkte, die mit "Kamera" getaggt sind.
Aber das ist sicher nicht der richtige Weg. Wenn Sie mehr als 10 Tags haben, wird Ihnen langweilig.

Also die Idee ist es, ein Modul zu machen, dass Magento Tags wie tag/something erkennen lässt und die Links für Tags auf das gleiche Format oben ändert, so dass Sie nicht viele Vorlagen bearbeiten müssen.
Ich nannte das Modul Easylife_Tag. Sie benötigen dafür die folgenden Dateien.

app/etc/modules/Easylife_Tag.xml - die Deklarationsdatei

<?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 - die Konfigurationsdatei

<?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 - das neu geschrieben tag Modell

<?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 - neu geschrieben Tag Ressourcenmodell

<?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 - der Brauch Router - siehe Kommentare Inline

<?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; 
    } 
} 

Das ist es. Löschen Sie den Cache und versuchen Sie es.

[EDIT].
You can find the full extension here. Der einzige Unterschied besteht darin, dass der Codepool community statt lokal wie oben beschrieben verwendet wird.

+1

Marius, du bist ein Magento Gott –

+0

@DavidWilkins Cut it out. Du lässt mich rot werden. :) – Marius

+0

Wie hast du es geschafft, die 'setAlias ​​(...' Teil am Ende? Magento hat keinen automatisierten Test, gibt es eine bestimmte Reihe von manuellen Tests, die Sie laufen, um diese Art von Sachen zu fangen? Ehrlich gesagt Ich habe keine Ahnung, wie man in Magento sicher verschlüsselt, und dieses hier hat mich einfach umgehauen. – shampoo

3

Ich benutze Magento 1.8.1 und versuchte die Marius-Lösung, aber ich hatte ein Problem: 2 + Keyword-Tags (mit einem Leerzeichen zwischen Wörtern) ging 404 Seite und die Leerzeichen in URL wurden in% 20 geändert. Ein Keyword-Tag funktioniert wie ein Zauber!

Also änderte ich sein Modul, um ein Leerzeichen auf Tag-Modul anzuzeigen und in der URL "hyphenized".

Datei: Easylife/Tag/Modell/Tag.php

return Mage::getUrl('', array('_direct' => 'tag/'.str_replace(" ", "-", $this->getName()))); 

Datei: Easylife/Tag/Controller/router.php

$tagName = str_replace("-", " ", $parts[1]); //tag name 

Sein für mich arbeiten jetzt.

Mit freundlichen Grüßen und vielen Dank für das Modul Marius!

Verwandte Themen