【问题标题】:How to get a url parameter in Magento controller?如何在 Magento 控制器中获取 url 参数?
【发布时间】:2013-10-06 17:21:30
【问题描述】:

是否有一个 Magento 函数可以从此 url 获取“id”的值:

http://example.com/path/action/id/123

我知道我可以在“/”上拆分 url 以获取值,但我更喜欢单个函数。

这不起作用:

$id = $this->getRequest()->getParam('id');

只有当我使用http://example.com/path/action?id=123时才有效

【问题讨论】:

  • $id = $this->getRequest()->getParam('id');这仅适用于扩展至 Mage_Adminhtml_Controller_Action(在管理员上)或 Mage_Core_Controller_Front_Action(在前端)的类。如果这不起作用,则意味着您的控制器没有被调用。

标签: magento url controller


【解决方案1】:

Magento 的默认路由算法使用 三部分 URL。

http://example.com/front-name/controller-name/action-method

所以当你打电话时

http://example.com/path/action/id/123

path 是你的名字,action 是你的控制器名,id 是你的动作方法。 这三种方法之后,就可以使用getParam来抓取键/值对了

http://example.com/path/action/id/foo/123

//in a controller
var_dump($this->getRequest()->getParam('foo'));

您也可以使用getParams 方法来获取参数数组

$this->getRequest()->getParams()

【讨论】:

  • 我明白了。但是我认为问题还在于我没有定义路由器。我正在使用重写:
  • /mymod /mycon
【解决方案2】:

如果你的网址是以下结构:http://yoursiteurl.com/index.php/admin/sales_order_invoice/save/order_id/1795/key/b62f67bcaa908cdf54f0d4260d4fa847/

然后使用:

echo $this->getRequest()->getParam('order_id'); // output is 1795

如果您想获取所有 URL 值或参数值,请使用以下代码。

var_dump($this->getRequest()->getParams());

如果你的网址是这样的:http://magentoo.blogspot.com/magentooo/userId=21

然后用这个来获取url的值

echo $_GET['userId'];

如果您想了解有关此click here 的更多信息。

【讨论】:

    【解决方案3】:

    如果是 Magento 模块,您可以使用 Varien Object getter。如果是针对自己的模块控制器,可能需要使用 register 方法。

    来源:http://www.vjtemplates.com/blog/magento/register-and-registry

    【讨论】:

      猜你喜欢
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      • 2018-09-13
      • 1970-01-01
      • 2015-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多