让事件在cakephp 3.4中工作
我正试图让cakephp的事件功能在CakePHP3.4中工作,我花了几个世纪才弄明白。我将非常感激能得到的一切帮助。 我希望在保存用户信息后将验证电子邮件发送给用户。 在用户控制器>>寄存器方法中让事件在cakephp 3.4中工作,php,cakephp,cakephp-3.4,Php,Cakephp,Cakephp 3.4,我正试图让cakephp的事件功能在CakePHP3.4中工作,我花了几个世纪才弄明白。我将非常感激能得到的一切帮助。 我希望在保存用户信息后将验证电子邮件发送给用户。 在用户控制器>>寄存器方法中 public function register() { // $this->viewBuilder()->getLayout('logo-only'); $user = $this->Users->newEntity();
public function register()
{
// $this->viewBuilder()->getLayout('logo-only');
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->getData());
if($this->Users->processRegistration($user)) {
}else {
$this->Flash->error(__('Your account was not created, check highlighted form fields to correct errors'));
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
在我的用户表中
public function initialize()
{
parent::intialize();
$event = new UserListener();
$this->eventManager()->on($event);
}
public function processRegistration ($entity)
{
if ($this->save($entity)) {
$event = new Event('Model.Users.afterRegister', $this, ['user' => $entity]);
$this->eventManager()->dispatch($event);
return true;
}
return false;
}
在我的src/Event/UserListener.php中
<?php
namespace App\Event;
use Cake\Log\Log;
use Cake\Event\EventListenerInterface;
use Cake\Mailer\Email;
class UserListener implements EventListenerInterface {
public function implementedEvents () {
return [
'Model.User.afterRegister' => 'afterRegister'
];
}
public function afterRegister ($event, $user)
{
$email = new Email('default');
$email->setFrom(['support@example.com' => 'Example Site'])
->setTo('admin@example.com')
->setSubject('New User Registration - ' . $user['username'])
->send('User has registered in your application');
}
}
?>
希望我能够理解到可以提供帮助的一点。感谢您的帮助现在工作很好;
我在侦听器中实现了“Model.User.afterRegister”,但正忙于调用我的UserTable中的“Model.Users.afterRegister”。
希望你也能从中学到一些东西#蒙着眼睛微笑