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

时间:2013-12-19 06:45:51

标签: php magento magento-1.7

我已在自定义模块页面中设置了产品收集的以下页面,该页面来自以下URL

http://localhost/magento/brand/htc
来自frontend.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>

来自Controller / Router.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

相反,我需要在产品集合中使用url

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

如何在产品系列中设置/实现路由网址?

2 个答案:

答案 0 :(得分:0)

我认为将控制器的“frontName”设置为“brand”将是在不使用自定义路由器的情况下实现此目的的最简单方法。

<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>

答案 1 :(得分:0)

在config.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>

来自Toolbar.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:)