【问题标题】:Make a good url without the index with magento用magento制作一个没有索引的好网址
【发布时间】:2013-10-22 04:13:47
【问题描述】:

我想制作一个 phtml 文件来显示特定品牌的产品。

网址需要如下所示:

brands/Subway
brands/Mcdonalds
brands/Lego
brands/Barbie
brands/Duplo
brands/.....

我怎样才能使这个网址在品牌是变量的情况下工作。

我试图制作一个模块,但在这种情况下,我得到了一个类似品牌/索引/品牌/地铁的网址

我也不想为每个品牌 (100+) 重写

【问题讨论】:

    标签: php magento url-rewriting module rewrite


    【解决方案1】:

    你可以在你的模块中创建一个自定义路由器来处理像brands/Subway这样的路由。
    这是你需要的。

    首先,您需要使页面无需重写即可正常工作。
    让我们假设在这个 url mysite.com/brands/index/index - 你有品牌列表
    在网址mysite.com/brands/index/view/id/12 上 - 您拥有 id 为 12 的品牌。

    在您的 config.xml 中,您需要将其添加到 <global> 标记中:

    <events>
        <controller_front_init_routers>
            <observers>
                <[namespace]_[module]>
                    <class>[Namespace]_[Module]_Controller_Router</class>
                    <method>initControllerRouters</method>
                </[namespace]_[module]>
            </observers>
        </controller_front_init_routers>
    </events>
    

    这将为现有路由器添加一个新路由器。 现在路由器类app/code/local/[Namespace]/[Module]/Controller/Router.php

    <?php 
    class [Namespace]_[Module]_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract{
        public function initControllerRouters($observer){
            $front = $observer->getEvent()->getFront();
            $front->addRouter('[module]', $this);
            return $this;
        }
        public function match(Zend_Controller_Request_Http $request){
            if (!Mage::isInstalled()) {
                Mage::app()->getFrontController()->getResponse()
                    ->setRedirect(Mage::getUrl('install'))
                    ->sendResponse();
                exit;
            }
            $urlKey = trim($request->getPathInfo(), '/');
            $check = array();
            $parts = explode('/', $urlKey);
            if ($parts[0] == 'brands'){//if url key starts with 'brands'
                if (!isset($parts[1])){ //if the url is just 'brands' - then redirect to brand list
                    return false;//do nothing...the request will be handled by the default router and you will see `brands/index/index/`.
                }
                elseif ($parts[1] == 'index'){//if your controller is named differently change 'index' to your controller name
                    return false; //do nothing...the request will be handled by the default router and you will see `brands/index/index/`.
                }
                else {
                    //if a name is specified
                    //get the entities with that name.
                    $collection = Mage::getModel('[module]/[entity]')->getCollection()
                        //if the field is named differently change 'name' to your field name.
                        ->addAttributeToFilter('name', $parts[1]);
                    $collection->getSelect()->limit(1);//limit to only one
                    $itemId = (int)$collection->getFirstItem()->getId();
                    //redirect to 'brands/index/view/id/$itemId'
                    $request->setModuleName('brands')
                        ->setControllerName('index') //if your controller is named differently change 'index' to your controller name
                        ->setActionName('view') //if your action is named differently change 'view' to action name
                        ->setParam('id', $itemId);
                    $request->setAlias(
                        Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
                        $urlKey
                    );
                    return true;
                }
            }
            return false;
        }
    }
    

    就是这样。清空缓存试试看。

    【讨论】:

    • 我无法让它工作。现在网址是/merk/index/view/name/Subway。我已经与观察者&lt;global&gt; &lt;controller_front_init_routers&gt; &lt;observers&gt; &lt;SpeelgoedwinkelXL_Brands&gt; &lt;class&gt;SpeelgoedwinkelXL_Brands_Controller_Router&lt;/class&gt; &lt;method&gt;initControllerRouters&lt;/method&gt; &lt;/SpeelgoedwinkelXL_Brands&gt; &lt;/observers&gt; &lt;/controller_front_init_routers&gt; &lt;/global&gt; 建立了一个全局关系,在 router.php 中,我将函数 intiControllerRouters 更改为 echo 'testing';出口;但它不会在屏幕上显示任何内容。还是不能调试路由器?
    • 对不起...我忘记了一个 xml 标签&lt;events&gt;。我已经更新了代码
    【解决方案2】:

    您可以在 .htaccess(根目录中的那个)中创建一个重写规则。

    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule ^brand/(.+)$ /brands/index/brands?brand=$1 [L,R=301,QSA]
    

    如果您的网址是:

    www.yoursite.com/brands/index/brands?brand=MyBrandName

    你会看到:

    www.yoursite.com/brand/MyBrandName

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      相关资源
      最近更新 更多