在非对象上调用成员函数 success()


Call to a member function success() on a non-object

我已经以不同的方式多次尝试了这个错误,也尝试了一个解决方案:CakePHP:在非对象网址上调用成员函数setFlash()...但是这个解决方案在我的项目中也不起作用。

无法识别错误所在。!!

这是我的用户.php模型

class User extends AppModel {
public $helpers = array('Html','Form');
public $components = array('Email','Flash','Session');

 public function send_mail($useremail){
    $Email = new CakeEmail('gmail');
    $Email->emailFormat('html')
          ->from('abc@xyz.com')
          ->to($useremail)
          ->subject('User subject');
    if($Email->send("123")){
        $this->Flash->success(__('Mail Sent')); // **this line cause error**
    }else{
        $this->Session->setFlash('Problem during sending email');
    }
}
}

正如文档所建议的,FlashComponent提供了两种设置flash消息的方法:它的__call()魔术方法和它的 set()方法。为了提供应用程序的详细程度,FlashComponent 的 __call() 魔术方法允许您使用映射到位于src/Template/Element/Flash directory下的元素的方法名称。按照惯例,驼峰方法将映射到小写和带下划线的元素名称:

使用 src/Template/Element/Flash/success.ctp

$this->Flash->success('This was successful');

使用 src/Template/Element/Flash/great_success.ctp

$this->Flash->greatSuccess('This was greatly successful');

或者,要设置纯文本消息而不呈现元素,可以使用 set() 方法:

$this->Flash->set('This is a message');

因此,您需要通过键入来更改它

 $this->Flash->success('Mail sent');