如何在Magento控制器中获取url参数?

jog*_*i99 17 url controller magento

是否有Magento函数从此url获取"id"的值:

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

我知道我可以在"/"上拆分url来获取值,但我更喜欢单个函数.

这不起作用:

$id = $this->getRequest()->getParam('id');
Run Code Online (Sandbox Code Playgroud)

它只适用于我使用http://example.com/path/action?id=123

Ala*_*orm 42

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

http://example.com/front-name/controller-name/action-method
Run Code Online (Sandbox Code Playgroud)

所以当你打电话

http://example.com/path/action/id/123
Run Code Online (Sandbox Code Playgroud)

这个词path是你的名字,action是你的控制器名称,id是你的行动方法. 这三种方法之后,您可以使用getParam获取键/值对

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

//in a controller
var_dump($this->getRequest()->getParam('foo'));
Run Code Online (Sandbox Code Playgroud)

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

$this->getRequest()->getParams()
Run Code Online (Sandbox Code Playgroud)


use*_*094 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
Run Code Online (Sandbox Code Playgroud)

如果您想获取所有Url值或参数值,请使用下面的代码.

var_dump($this->getRequest()->getParams());
Run Code Online (Sandbox Code Playgroud)

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

然后使用它来获取url的值

echo $_GET['userId'];
Run Code Online (Sandbox Code Playgroud)

如果您想了解更多信息,请点击此处.