0

I created a custom module to fetch data from an external API and I would like to display it on the frontend.

I put my curl function in Helper\Data.php - Is it the best place to put it?

I called Helper\Data in my Block\Display.php but in the templates\display.phtml. It will show as blank.

Thank you in advance.

Block File:- Block\Display.php

  <?php

namespace NzPost\AddressFinder\Block;

class Display extends \Magento\Framework\View\Element\Template
{
    /**
     * @var \NzPost\AddressFinder\Helper\Data
     */
    private $helperData;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \NzPost\AddressFinder\Helper\Data $helperData
    )
    {
        $this->helperData = $helperData;
        parent::__construct($context);
    }

    public function sayHello()
    {
        $client_id = $this->helperData->getGeneralConfig('client_id');
        $client_secret = $this->helperData->getGeneralConfig('client_secret');
        $result = $this->helperData->getCurl($client_id, $client_secret);
        return __($result);
    }
}

Template File:-display.phtml

    <?php
echo $this->sayHello();
0

try below code to show curl result on the template file.

Block Code

<?php

namespace NzPost\AddressFinder\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use NzPost\AddressFinder\Helper\Data;

/**
 * Class Display
 */
class Display extends Template
{

    /**
     * @var \NzPost\AddressFinder\Helper\Data
     */
    private $helperData;

    /**
     * Display constructor.
     * @param Context $context
     * @param array $data
     */
    public function __construct(
        Context $context,
        Data $helperData,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->helperData = $helperData;
    }

    /**
     * @return Array
     */
    public function sayHello()
    {
        $client_id = $this->helperData->getGeneralConfig('client_id');
        $client_secret = $this->helperData->getGeneralConfig('client_secret');
        $result = $this->helperData->getCurl($client_id, $client_secret);

        return $result;
    }


}

Template Code

$CurlData = $block->sayHello();
var_dump($CurlData);
2
  • Hi @Devidas, I have tried this as well. But nothing shows up. If I add anything in the __construct(context $context, abc $abc) the phtml page will always show blank. – Tony Le Apr 26 '20 at 22:31
  • Hey, @TonyLe please check my block code I have updated. – Devidas Apr 27 '20 at 5:40

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.