Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.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
Php 如何在Magento自定义模块中设置产品集合的自定义URL?_Php_Magento_Magento 1.7 - Fatal编程技术网

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

Php 如何在Magento自定义模块中设置产品集合的自定义URL?,php,magento,magento-1.7,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</t

我在自定义模块页面中为产品集合设置了以下页面,该页面来自以下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>
从块文件

...
    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中列出产品集合,我可以看到如下url

> http://localhost/magento/shop/index/pageview/identifier/htc/?mode=list
相反,我需要在产品集合中的url作为

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

如何在product collection中设置/acheive路由url?

我认为在不使用自定义路由器的情况下,将控制器的“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>

前端
测试车间控制器路由器

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

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

谢谢,我在config.xml中使用了frontname设置。这个frontname将动态地来自管理配置项:)。所以我使用了自定义路由。当你说url“即将到来”时,你是什么意思。在模板中创建URL时,路由器不会更改URL的构建方式。要测试路由器,您需要手动键入该URL,路由器应匹配该URL并设置正确的控制器/操作。只要您有自己的控制器,您就有责任在控制器/块中实现产品列表、筛选、排序、网格视图等。该行为不会自动从其他地方“传递”或“继承”下来您必须根据URL中收到的参数添加筛选和排序。如果您希望使用与产品列表相同的功能,您必须使您的块扩展产品列表块并覆盖其
\u toHtml
方法或其他一些方法,并将您的品牌过滤器添加到
$this->\u productCollection
,后者已经有许多自己的过滤器(处理URL过滤、排序等)。
<default>
    <web>
        <routers>
            <shop_router>
                <area>frontend</area>
                <class>Test_Shop_Controller_Router</class>
            </shop_router>
        </routers>
    </web>
</default>
 <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>
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);
    }


}