2013-10-06 58 views
11

有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

+1

$ id = $ this-> getRequest() - > getParam('id');這隻能在擴展到Mage_Adminhtml_Controller_Action(在管理員)或Mage_Core_Controller_Front_Action(在前端)上的類中使用。如果這不起作用,這意味着你的控制器沒有被調用。 –

回答

36

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() 
+0

我明白了。但是我認爲這個問題也是因爲我沒有定義路由器。我使用的是重寫: – jogi99

+0

​​ <![CDATA [#^ /?AJAX /產品/項目#]]> /mymod/mycon jogi99

6

數組如果您的網址是結構如下: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']; 

的價值。

相關問題