【问题标题】:how to build query string in magento如何在magento中构建查询字符串
【发布时间】:2012-07-05 10:53:16
【问题描述】:

您好想在 magento 中构建查询字符串。我试过了

<?php 
echo $this->getUrl("catalog/category/view",
  array(
    "_use_rewrite"=>false,
    "category"=>$_category->getId(),
    "product"=>$_product->getId()
  )
);
?>

我想要网址: http://www.localhost.com/hungermunch/fujigrill/catalog/category/view?category=11&amp;product=1 但我得到了

http://www.localhost.com/hungermunch/fujigrill/catalog/category/view/category/11/product/1/

我怎样才能获得所需的网址。有没有可能

【问题讨论】:

  • 但是为什么你使用重写只是在类别 url 之后添加你想要的内容
  • 我想通过 ajax 发送数据,所以我认为这样做很容易。如果有其他方法
  • 使用类似 $this->getUrl("catalog/category/view")?product=pid&category=cid
  • 嗨 samit rimal 我已经投票赞成你所有的问题和答案你的声誉现在从 15 增加到 57..现在很高兴..

标签: php magento-1.6


【解决方案1】:

您还可以像这样在 Magento 中附加 url 查询字符串参数:

$params = array(
    '_query' => array(
        'category' => $_category->getId(),
        'product'  => $_product->getId(),
    )
);

echo Mage::getUrl('catalog/category/view', $params);

这里是 getUrl() 方法的参考:

http://www.magentocommerce.com/wiki/5_-_modules_and_development/reference/geturl_function_parameters

【讨论】:

    【解决方案2】:

    如果你想在最后添加一个查询字符串,你可以使用这个方法:

    $this->getUrl("catalog/category/view") . "?" .
    http_build_query(
        "category" => $_category->getId(),
        "product"  => $_product->getId()
    );
    

    虽然我认为没有理由不使用您的第一种方法,然后根据需要从 Magento 获取值,例如在控制器中您可以这样做:

    $productId = $this->getRequest()->getParam('product');
    $categoryId = $this->getRequest()->getParam('category');
    

    Magento 然后会从您的代码生成的 url 中为您获取这些值。

    【讨论】:

      猜你喜欢
      • 2012-01-17
      • 2013-05-30
      • 2010-10-24
      • 2010-09-23
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多