如何在Magento自定义模块中为产品集合设置自定义URL


How to set my custom URL for product collection in Magento custom module?

>我在自定义模块页面中为产品集合设置了以下页面,这些页面来自以下网址

http://localhost/magento/brand/htc

前端.xml文件中的 XML

<shop_index_pageview>
        <reference name="root">
            <action method="setTemplate"><template>page/2columns-left.phtml</template></action>
        </reference>
        <reference name="content">
            <block type="shop/shop" name="testattrproduct" template="shop/product.phtml" />
            <block type="shop/shop" name="shop" template="catalog/product/list.phtml">
              <block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
                  <block type="page/html_pager" name="product_list_toolbar_pager" />
              </block>
              <action method="setColumnCount"><column_count>6</column_count></action>
              <action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
              <action method="addColumnCountLayoutDepend"><layout>empty</layout><count>6</count></action>
              <action method="addColumnCountLayoutDepend"><layout>one_column</layout><count>5</count></action>
              <action method="addColumnCountLayoutDepend"><layout>two_columns_left</layout><count>4</count></action>
              <action method="addColumnCountLayoutDepend"><layout>two_columns_right</layout><count>4</count></action>
              <action method="addColumnCountLayoutDepend"><layout>three_columns</layout><count>3</count></action>
              <action method="setToolbarBlockName"><name>product_list_toolbar</name></action>     
            </block>              
      </reference>      
    </shop_index_pageview>

使用路由,我已经设置了要使用的模块,控制器,操作。

<events>
        <controller_front_init_routers>
            <observers>
                <test_initRouter>
                    <type>singleton</type>
                    <class>Test_Shop_Controller_Router</class>
                    <method>initController</method>
                </test_initRouter>
            </observers>
        </shop_initRouter>
        </events>

来自控制器/路由器的文件.php

            ...
            ...
    public function match(Zend_Controller_Request_Http $request)
{  
        $front = $this->getFront(); 
    $identifier = trim($request->getPathInfo(), '/');  
    if ($identifier) {
        $p = explode('/', $identifier);
    } else {
        $p = explode('/', $this->_getDefaultPath());
    }    
        ...
            ...
    if(substr('brand', 0, strlen('brand')) =='brand'){ 
        if($p[1] == '')
            {
                $action = "view";
                $param = $action;
            }
        else
            {
                $action = "pageview";
                $param = $p[1];
            }
        $request->setModuleName('shop')
            ->setControllerName('index')
            ->setActionName(trim($action, '/'));
        if($p[1] == '')
            $request->setParam('param', $param);
        else
            $request->setParam('identifier', $param);
        return true;
    } 
}       
            ...
            ...

从块文件

...
    protected function _getProductCollection() {
        if (is_null($this->_productCollection)) {
            $_getCurrentAttrObj = $this->_selected_attribute;
            // d($_getCurrentAttrObj->getData());
            if($_getCurrentAttrObj){
                $_defConfigAttrId = Mage::getStoreConfig('shop_by_brand/all_brands_page_setup/attribute_list');
                $arg_attribute = Mage::helper("shop")->getAttributeNameById($_defConfigAttrId);
                $collection = Mage::getModel('catalog/product')
                    ->getCollection('*')
                    ->addAttributeToSelect('*');
                $collection->addAttributeToFilter('test',12);
                Mage::getModel('catalog/layer')->prepareProductCollection($collection);
                $this->_productCollection = $collection;
            }
        }
        return parent::_getProductCollection();
    }   
....

这是意料之中的。我可以从 product.phtml 列出产品集合在这里我可以看到如下所示的网址

> http://localhost/magento/shop/index/pageview/identifier/htc/?mode=list

相反,我需要产品集合中的网址作为

> http://localhost/magento/brand/htc/?mode=list

如何在产品集合中设置/获取路由 URL?

我认为将

控制器的"frontName"设置为"品牌"将是不使用自定义路由器即可实现这一目标的最简单方法。

<routers>
        <Example>
            <use>standard</use>
            <args>
                <module>Example</module>
                <frontName>Cool</frontName>
            </args>
        </Example>
 </routers>

但是,如果路由逻辑对于您的特定用例来说更复杂,那么您应该正确定义路由器,而不是作为事件观察者。

<default>
    <web>
        <routers>
            <shop_router>
                <area>frontend</area>
                <class>Test_Shop_Controller_Router</class>
            </shop_router>
        </routers>
    </web>
</default>

在配置中使用覆盖或重写.xml

 <global>
    <blocks>
      <shop>
        <class>Test_Shop_Block</class>
      </shop>
     <catalog>
        <rewrite>
          <product_list_toolbar>Test_Shop_Block_Product_List_Toolbar</product_list_toolbar>
        </rewrite>
      </catalog>      
    </blocks>
  </global>

和从工具栏.php

class Test_Shop_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{
    public function getPagerUrl($params=array())
    {
        $getDefaultUrl = new Magecart_Shopbyattribute_Block_Shop();
        $getDefaultUrlKeyForAttr = $getDefaultUrl->getDefaultUrlKeyForAttr();
        $_params = $this->getRequest()->getParams();
        $identifier = $_params['identifier'];
        $urlParams = array();
        $urlParams['_current']  = false;
        $urlParams['_escape']   = true;
        $urlParams['_use_rewrite']   = true;
        $urlParams['_query']    = $params;
        return $this->getUrl($getDefaultUrlKeyForAttr.'/'.$identifier, $urlParams);
    }

}

就是这样。现在 magento 将使用来自 getPagerUrl() 函数的 url :)