19

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

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

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

这不起作用:

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

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

4

3 回答 3

48

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()
于 2013-10-06T22:33:18.447 回答
7

如果您的网址是以下结构: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'];

如果您想了解有关此的更多信息,请单击此处

于 2014-01-24T04:50:00.723 回答
0

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

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

于 2013-10-06T21:53:40.433 回答