有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(在前端)上的類中使用。如果這不起作用,這意味着你的控制器沒有被調用。 –