3
<?php

namespace Vendor\Module\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{

    protected $resultPageFactory;

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

    public function execute()
    {

    }
}
3

You could use regular JSON response class:

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;

class Test extends Action
{

    /**
     * @var \Magento\Framework\Controller\Result\JsonFactory
     */
    protected $resultJsonFactory;

    public function __construct(
        Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
    ) {
        parent::__construct($context);
        $this->resultJsonFactory = $resultJsonFactory;
    }

    /**
     * Dispatch request
     *
     * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
     * @throws \Magento\Framework\Exception\NotFoundException
     */
    public function execute()
    {
        /** @var \Magento\Framework\Controller\Result\Json $result */
        $result = $this->resultJsonFactory->create();
        $resultData = [
            'any_key' => 'Any data goes here',
            'success' => true,
            'time' => time(),
            'replace_button_message' => __('Your store has been already registered'),
        ];

        return $result->setData($resultData);
    }
}
  • thanks for reply @Siarhey Uchukhlebau i am getting error with your code Recoverable Error: Argument 2 passed – Rahul Katoch Mar 22 '17 at 7:55
  • @Rahulocodewire try to clear your var/generation directory – Siarhey Uchukhlebau Mar 22 '17 at 7:56
  • @Rahulocodewire Could you add here a full error trace? – Siarhey Uchukhlebau Mar 22 '17 at 7:57
  • 1
    @Rahulocodewire You should remove old generated files: rm -rf var/generation/* from the magento root directory. – Siarhey Uchukhlebau Mar 22 '17 at 8:11
  • 1
    @Rahulocodewire does it helped you? – Siarhey Uchukhlebau Mar 22 '17 at 8:55
2

You can do it using following code:

<?php

namespace Vendor\Controller\Controller\Index;

use \Magento\Framework\App\Action\Action;

class Index extends Action
{
    public function execute()
    {
        $jsonResult = ['data' => 'test'];

        $this->getResponse()->representJson(
            $this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($jsonResult)
        );
    }
}
  • thanx for reply can you please post full controller – Rahul Katoch Mar 22 '17 at 7:02
  • 1
    No problem, I've updated my answer. – Bartłomiej Szubert Mar 22 '17 at 7:06
  • thanks its worked but i actually i want get response from api when i print the request i get only empty array – Rahul Katoch Mar 22 '17 at 7:28
  • api send data in my controller url can you please tell me how i get the responce data from api in my controller ? – Rahul Katoch Mar 22 '17 at 7:29
0

Try This Code

$this->getResponse()->representJson($this->_objectManger->get(Your Path::class)->JsonEncode($jsonResult));

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.