如何在 Magento 控制器中获取 url 参数? [英] How to get a url parameter in Magento controller?
问题描述
是否有一个 Magento 函数可以从这个 url 中获取id"的值:
Is there a Magento function to get the value of "id" from this url:
http://example.com/path/action/id/123
我知道我可以在/"上拆分 url 来获取值,但我更喜欢单个函数.
I know I can split the url on "/" to get the value, but I'd prefer a single function.
这不起作用:
$id = $this->getRequest()->getParam('id');
它仅在我使用 http://example.com/path/action?id=123 时有效
It only works if I use http://example.com/path/action?id=123
推荐答案
Magento 的默认路由算法使用三部分 URL.
Magento's default routing algorithm uses three part URLs.
http://example.com/front-name/controller-name/action-method
所以当你打电话
http://example.com/path/action/id/123
path
是你的前台名称,action
是你的控制器名称,id
是你的操作方法.这三个方法之后,就可以使用getParam
来抓取键值对
The word path
is your front name, action
is your controller name, and id
is your action method. After these three methods, you can use getParam
to grab a key/value pair
http://example.com/path/action/id/foo/123
//in a controller
var_dump($this->getRequest()->getParam('foo'));
你也可以使用getParams
方法来获取参数数组
You may also use the getParams
method to grab an array of parameters
$this->getRequest()->getParams()
这篇关于如何在 Magento 控制器中获取 url 参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!