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);
}
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.

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);
  • 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 – user3443146 Dec 20 '16 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 – Ankit Shah Dec 20 '16 at 8:27
  • hi can you help me of this question magento.stackexchange.com/questions/151482/… – user3443146 Dec 22 '16 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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