致命错误在magento 2中调用我的Block时调用成员函数dispatch()的致命错误


19

这是我的阻止文件:

 <?php

 namespace ChennaiBox\Mymail\Block\Mail;

 class MailContent extends \Magento\Framework\View\Element\Template
 {
 protected $_objectManager;

 protected $customerSession;

 public function __construct(
    \Magento\Customer\Model\Session $customerSession,  
    \Magento\Framework\ObjectManagerInterface $objectManager
 ) {
    $this->customerSession = $customerSession;
    $this->_objectManager = $objectManager;
  }

 public function mymailData()
 {
try{

     if ($this->customerSession->isLoggedIn()) {
     $cutomerEmail    =(string)$this->customerSession->getCustomer()->getEmail();

     echo $cutomerEmail;

      else{
            $this->_redirect('customer/account/login/');
          }
   }catch (Exception $e) {

        $e->getMessage();

    }
   }

 }

如果我叫这个方块我会出错

PHP致命错误:调用到/var/www/html/magento2/vendor/magento/framework/View/Element/AbstractBlock.php成员函数派遣()在零线642,引用者: HTTP://magentodev.gworks .mobi / magento2 / customer / account / index /

从apache error.log文件中提取。为什么,建议我如何解决此问题。

Answers:


38

问题在于您的构造函数与父类构造函数不匹配。

要解决此问题,您需要更新构造函数:

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Customer\Model\Session $customerSession,  
    \Magento\Framework\ObjectManagerInterface $objectManager,
    array $data = []
 ) {
    parent::__construct($context, $data);
    $this->customerSession = $customerSession;
    $this->_objectManager = $objectManager;
  }

不要忘记在更改后刷新var/cachevar/generation


1
谢谢。这帮助了我其中一种“我知道我忘记了一些东西,但我不记得发生了什么”的情况。
Siliconrockstar
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.