URL Magento2からパラメータを取得します



Get Parameters From Url Magento 2



解決:

ObjectManagerによる:

$ objectManager =  Magento  Framework  App  ObjectManager :: getInstance(); $ request = $ objectManager-> get( 'Magento  Framework  App  Request  Http'); echo $ param = $ request-> getParam( 'order_id');

ファクトリメソッドによる



保護された$ request; public function __construct(...  Magento  Framework  App  Request  Http $ request、...){$ this-> request = $ request; } $ this-> request-> getParam( 'order_id');

注:Magento 2コーディング標準として、ファイル内でobjectManagerを直接使用しないでください。


まず、注入する必要があります Magento Framework App Request Http at_constructfunctionを使用して



保護された$ request; public function __construct( Magento  Framework  App  Request  Http $ request、.... //残りのパラメーターはここにあります){$ this-> request = $ request; ... //コンストラクタの残りはここにあります} public function getIddata(){// $ this-> request-> getParams();を使用します//すべてのパラメータは$ this-> request-> getParam( 'order_id');を返します}

BLockクラスでは、このクラスを注入する必要はありません。使用する

代わりに$ this-> getRequest()$ this-> request。


=>ファクトリメソッド:



リクエスト= $ request; ... //コンストラクタの残りはここにあります} public function getPost(){return $ this-> request-> getParam( 'order_id'); }}

=>オブジェクトマネージャ:

$ objectManager =  Magento  Framework  App  ObjectManager :: getInstance(); $ request = $ objectManager-> get( 'Magento  Framework  App  Request  Http'); echo $ request-> getParam( 'order_id');

=>注: Magentoのコーディング形式としてダイレクトオブジェクトマネージャーを使用しないでください