有Magento的功能,从这个URL获得“ID”的值:如何在Magento控制器中获取url参数?
http://example.com/path/action/id/123
我知道我可以在“/”来获取值拆分URL,但我更喜欢一个单一功能。
这不起作用:
$id = $this->getRequest()->getParam('id');
如果我使用http://example.com/path/action?id=123
有Magento的功能,从这个URL获得“ID”的值:如何在Magento控制器中获取url参数?
http://example.com/path/action/id/123
我知道我可以在“/”来获取值拆分URL,但我更喜欢一个单一功能。
这不起作用:
$id = $this->getRequest()->getParam('id');
如果我使用http://example.com/path/action?id=123
Magento的默认路由算法使用三部分组成网址。
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()
如果它是一个Magento的模块,它仅适用,您可以使用瓦瑞恩对象吸气。如果它是用于您自己的模块控制器,则可能需要使用注册方法。
来源:http://www.vjtemplates.com/blog/magento/register-and-registry
数组如果您的网址是结构如下: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
然后使用这个,如果你想了解这个click here更多信息以获取URL
echo $_GET['userId'];
的价值。
$ id = $ this-> getRequest() - > getParam('id');这只能在扩展到Mage_Adminhtml_Controller_Action(在管理员)或Mage_Core_Controller_Front_Action(在前端)上的类中使用。如果这不起作用,这意味着你的控制器没有被调用。 –