如何将shell函数调用到另一个控制器-Cakephp
我在shell中使用此功能发送电子邮件 编辑: 用户shell如何将shell函数调用到另一个控制器-Cakephp,shell,email,cakephp,controller,Shell,Email,Cakephp,Controller,我在shell中使用此功能发送电子邮件 编辑: 用户shell <?php namespace App\Shell; use Cake\Console\Shell; use Cake\Log\Log; use Cake\Controller\Component; use Cake\Controller\ComponentRegistry; use App\Controller\Component\EmailComponent; class UsersShell extends Shel
<?php
namespace App\Shell;
use Cake\Console\Shell;
use Cake\Log\Log;
use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
use App\Controller\Component\EmailComponent;
class UsersShell extends Shell
{
public function initialize()
{
parent::initialize();
$this->loadModel('Users');
//Load Component
$this->Email = new EmailComponent(new ComponentRegistry());
}
public function mail()
{
$to = 'exemple@gmail.com';
$subject = 'Hi buddy, i got a message for you.';
$message = 'User created new event';
try {
$mail = $this->Email->send_mail($to, $subject, $message);
print_r($mail);
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail-
>ErrorInfo;
}
exit;
}
如果我错了,请纠正我。首先,您的shell必须像这样启动
class UsersShell extends AppShell {
public function main(){ //change name here to main
$to = 'exemple@gmail.com';
$subject = 'Hi buddy, i got a message for you.';
$message = 'User created new event';
try {
$mail = $this->Email->send_mail($to, $subject, $message);
print_r($mail);
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
exit;
}
}
顺便说一下,如果要检查输出,必须返回类似于true
或false
的内容。否则,在执行shell之后没有必要检查输出
首先检查在CakePHP CLI中运行的Shell命令。像这样
如果shell命令成功运行。贝壳类的好
接下来在控制器中使用Shell
<?php
namespace FullCalendar\Controller;
use FullCalendar\Controller\FullCalendarAppController;
use Cake\Routing\Router;
use Cake\Event\Event;
use Cake\Console\ShellDispatcher;
class EventsController extends FullCalendarAppController
{
public $name = 'Events';
public function add()
{
$event = $this->Events->newEntity();
if ($this->request->is('post')) {
$event = $this->Events->patchEntity($event, $this->request->data);
if ($this->Events->save($event)) {
/* $shell = new ShellDispatcher();
$output = $shell->run(['cake', 'users'], ['plugin' =>
'Events']);
if (0 === $output) {
$this->Flash->success('Success from shell command.');
} else {
$this->Flash->error('Failure from shell command.'); */
$this->Flash->success(__('The event has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The event could not be saved. Please,
try again.'));
}
}
$this->set('eventTypes', $this->Events->EventTypes->find('list'));
$this->set(compact('event'));
$this->set('_serialize', ['event']);
$this->set('user_session', $this->request->session()-
>read('Auth.User'));
$this->viewBuilder()->setLayout('user');
}
不一定要检查我编辑的帖子,我不明白为什么它不工作,因为当我单独使用shell时,它工作得很好。阅读这个答案并用shell替换组件,它将是完全相同的。发送电子邮件也应该是一个孤立的对象,而不是组件。好的架构应该是创建一个事件,让一个侦听器在队列中创建一个条目来发送电子邮件,然后处理队列中的电子邮件。队列是一个一直在后台运行的worker shell。您的代码甚至有更多错误,不适合在这里使用。
class UsersShell extends AppShell {
public function main(){ //change name here to main
$to = 'exemple@gmail.com';
$subject = 'Hi buddy, i got a message for you.';
$message = 'User created new event';
try {
$mail = $this->Email->send_mail($to, $subject, $message);
print_r($mail);
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
exit;
}
}
bin/cake users mail
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Console\ShellDispatcher;
class PagesController extends AppController
{
/**
* Run shell command
*/
public function run()
{
$shell = new ShellDispatcher();
$output = $shell->run(['cake', 'users', 'mail']);
// $output = $shell->run(['cake', 'users', 'mail', 'email']); // [pass arguments]
// debug($output);
if ($output === 0) {
echo "Shell Command execute";
} else {
echo "Failure form shell command";
}
exit;
}
}
/**
* Send Mail with shell command
*/
public function mail()
{
$to = 'mail@gmail.com';
$subject = 'Hi buddy, i got a message for you.';
$message = 'Nothing much. Just test out my Email Component using PHPMailer.';
$mail = $this->Email->send_mail($to, $subject, $message);
// debug($mail);
if ($mail['error'] === false) {
$this->out("Mail Successfully Sent For :: ". $to);
} else {
$this->abort("Mail Error.");
}
}