0

i was reading a book magento2 tutorial. When learning how to create module. 2 augments was receiving in _construct function of controller.

Actually i am talking about this 2

\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory

how to know when which augment i have to use? i.e when working with product, order, customer. for example i saw that in other tutorial for get product list i have to use some like productfactory

Below is complete code for that controller

public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}

2 Answers 2

0

You have to use RequestInterface

public function __construct(
  \Magento\Framework\App\Action\Context $context,
  \Magento\Framework\View\Result\PageFactory $resultPageFactory,
  \Magento\Framework\App\RequestInterface $request
) {
    $this->resultPageFactory = $resultPageFactory;
    $this->request = $request;
    parent::__construct($context);
}

public function execute(){
       $value = $this->request->getParam('yourkey');
}

Your ajax call is like:

$.ajax({
      url: {Yourcontrollerurl},
      data: {yourkey: $('#yourHtmlTagID').val()},
      type: 'post',
      ---
      });

don't forgot to recompile.

1
0

You can use

\Magento\Framework\App\RequestInterface $request

or can use

$this->getRequest()->getParam('Your Variable Name')

or

use Magento\Framework\App\Request\DataPersistorInterface;

$post = $this->getRequest()->getPostValue();
var_dump($post);
3
  • do you mean i can use this ----- \Magento\Framework\App\RequestInterface $request ---- instead them ---- \Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory Commented Dec 20, 2016 at 8:24
  • Apart from this 2 \Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory Add Request one in cortoller as @Chirag told in Answer
    – Jackson
    Commented Dec 20, 2016 at 8:27
  • hi can you help me of this question magento.stackexchange.com/questions/151482/… Commented Dec 22, 2016 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.