0

I need to call the controller function from block . in magento 2 i use the registry. In my block i wrote

public function __construct(
        Template\Context $context,

        \Magento\Catalog\Model\CategoryFactory $categoryFactory,            
        \Magento\Backend\Model\Session $session,
        \Magento\Framework\Registry $registry
    )
    {
        $this->_categoryFactory = $categoryFactory;
        parent::__construct($context, $data);
        $this->_cardsManageFactory = $cardsManageFactory;
        $this->session = $session;
        $this->_registry = $registry;
    }
public function getMydata()
{
 return  $this->registry->registry('controller_data');}

in my admin controller i wrote

 public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $resultPageFactory,
        \Magento\Framework\Registry $registry
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
        $this->_registry = $registry;
    }
public function execute()
{
     .....
     ......
     .....  some calculation did here
     $response = $calculation;
     $this->registry->register('controller_data', $response);
}

while i call block from phtml

$data = $block->getMydata();

i got error Notice: Undefined property:

  • can you please share controller's execute function – Ramkishan Suthar Dec 17 '18 at 8:55
1

you have define registry variable this

$this->_registry

but you are using below:

$this->registry

that's why you getting error:

try

$this->_registry->registry('controller_data');

  • i changed now i am not facing error . i got empty resonse . while i print response in block i got nothing it is blank – Sammu Sundar Dec 17 '18 at 9:38
  • i call registry in block and assign register in admin controller is this possible? – Sammu Sundar Dec 17 '18 at 9:47
  • Why do you want to set frontend registry to backend controller? – Rutvee Sojitra Dec 17 '18 at 10:00
  • I did same calculation in both controller and block , i call controller function via ajax. block code via phtml load. i need to simplify the code. so i need to call admin controller via block. while i search i found registry. so i try to found whether it is working or not – Sammu Sundar Dec 17 '18 at 10:55
  • So you can abstct controller for it and you can use at admin as well as at front side – Rutvee Sojitra Dec 17 '18 at 11:30
0

Please don't redefine the function name getData().

Changes the function name getData() to other like public function getMyData()

and call this phtml using $block->getMyData();

  • Now i got the empty resonse – Sammu Sundar Dec 17 '18 at 8:40
  • In your construct I can see you defined as $this->_registry and in method you are using $this->registry @SammuSundar – Keyur Shah Dec 17 '18 at 9:06
  • aah my mistake.. – Amit Bera Dec 17 '18 at 9:09
  • This solves error but it returns empty response – Sammu Sundar Dec 17 '18 at 9:43

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.