0

I have a little problem to call a function from my block to my controller. I need to get a function from my block locate in Mymodule/AdminPage/Block/Adminhtml/Contactslist.php help pls i did it before once but I fail in every attempt i make.

<?php
  namespace Mymodule\AdminPage\Controller\Adminhtml\AdminDescsdev;

  class Index extends \Magento\Backend\App\Action
  {
    protected $resultPageFactory;

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


    public function ajaxProcessGetDescReplaces()
    {
        die(jsonEncode(array(
            'descReplaces' => " function to call getAll() "
        )));
    }

    public function execute()
    {   
        return  $resultPage = $this->resultPageFactory->create();
    }
  }

here is my block :

<?php
namespace Mymodule\AdminPage\Block\Adminhtml;

use Magento\Backend\Block\Template;

class Contactslist extends \Magento\Backend\Block\Template
{

 public function __construct(
     \Magento\Backend\Block\Template\Context $context,
     \Mymodule\AdminPage\Model\ContactFactory $contactFactory,
     \Magento\Framework\View\Result\PageFactory $resultPageFactory,
     array $data = []
 ) {
      parent::__construct($context, $data);
      $this->contactFactory = $contactFactory;
      $this->resultPageFactory = $resultPageFactory;
 }

    public function getAll(){
    $data = array();
    $model = $this->contactFactory->create();
    $newData = $model->getCollection();
    $tabl = array();
    foreach ($newdata as $d )  {
        $tabl[] = array(
            'toreplace' => $d->getToreplace(),
            'replaceby' => $d->getReplaceby()
        );
    }
    return $tabl;
}
}
2

Just create instance through object manager and call your function

public function ajaxProcessGetDescReplaces()
{
    $blockInstance = $this->_objectManager->get('Mymodule\AdminPage\Block\Adminhtml\Contactslist');
    $blockInstance->getAll()
}
  • it didnt seemed to wrked so i tried to put that in the execute to see if it work but i have an error : $blockInstance = $this->_objectManager->get('Mymodule\AdminPage\Block\Adminhtml\Contactslist'); $final = $blockInstance->getAll(); var_dump($final); – Antoine553 Jul 10 '17 at 12:11
  • Let me know what error you got. – satya prakash patel Jul 10 '17 at 14:37
  • it's ok just a syntax error XD – Antoine553 Jul 10 '17 at 14:58
0

You can call Block function inside controller using below way,

$getBlock = $this->getLayout()->createBlock("Mymodule\AdminPage\Block\Adminhtml\Contactslist");
$getBlock->getAll();

You can call above lines in your execute() method of controller file.

  • i tried to call them in the execute with a var_dump but it didn't worked – Antoine553 Jul 10 '17 at 12:17

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.