我在这个特定的应用程序上使用CakePHP。通过add()
在网站上提交博客文章时,应通过pending_email()
向管理员发送通知电子邮件,但我收到以下错误:
Fatal error: Call to undefined function pending_email() in .../app/controllers/blog_posts_controller.php on line 134
以下是代码:
function add($blog_id = null) {
if (!empty($this->data)) {
switch ($this->params['form']['submit']) {
case 'draft':
$this->data['BlogPost']['status_id'] = DRAFT;
break;
case 'approval':
$this->data['BlogPost']['status_id'] = PENDING;
break;
case 'publish':
$this->data['BlogPost']['status_id'] = PUBLISHED;
break;
}
$this->data['BlogPost']['tags'] = $this->__tagCleanup($this->data['BlogPost']['tags']);
$this->BlogPost->create();
if ($this->BlogPost->save($this->data)) {
if ($this->data['BlogPost']['status_id'] == PUBLISHED) {
$this->__tagUp($this->data['BlogPost']['blog_id'], $this->data['BlogPost']['tags']);
}
// Send the e-mail notating that a blog is pending
pending_email($blog_id);
$this->Session->setFlash(__('The blog post has been saved', true));
$this->redirect(array('action' => 'index', $this->data['BlogPost']['blog_id']));
} else {
$this->Session->setFlash(__('The blog post could not be saved. Please, try again.', true));
}
}
// fetch blog_post context
$this->BlogPost->Blog->recursive = -1;
$blog = $this->BlogPost->Blog->read(array('id','title'),$blog_id);
$this->set('blog', $blog);
$this->data['BlogPost']['user_id'] = $this->user_id;
if (!is_null($blog_id)) {
$this->data['BlogPost']['blog_id'] = $blog_id;
}
}
function pending_email($id) {
if (!$id) {
$this->Session->setFlash(__('Invalid blog post', true));
$this->redirect(array('action' => 'index'));
}
$this->BlogPost->contain(array(
'User' => array('fields' => 'id', 'full_name', 'name', 'last_name'),
'Tag' => array('fields' => 'name'),
'BlogPostComment' => array('fields' => array('created', 'content')),
'BlogPostComment.User' => array('fields' => array('name', 'last_name')),
));
if(($blogPost = $this->BlogPost->read(null, $id)) == NULL){
$this->Session->setFlash(__('Invalid blog post', true));
$this->redirect(array('controller'=>'spotlights', 'action' => 'index'));
}
$this->set('blogPost', $this->BlogPost->read(null, $id));
$temp = $this->BlogPost->read(null, $id);
$this->BlogPost->Blog->recursive = -1;
//Blog information
$title = $temp['BlogPost']['title'];
$author = $temp['User']['full_name'];
$date_created = $temp['BlogPost']['created'];
// Properly format the excerpt
$excerpt = preg_replace("/&#?[a-z0-9]+;/i","", $temp['BlogPost']['content']);
$content = "<h2>Hello,</h2><p>$author has submitted a new blog post for review. <ul><li><b>Title:</b> $title </li><li><b>Author</b>: $author</li><li><b>Excerpt</b>: \"$excerpt\"</li><li><b>Created on:</b> $date_created</li></ul></p><p>You can log into the dashboard to approve this post.</p>";
$to = ADMIN_EMAIL;
$from = SMTP_FROM;
$subject = "New blog post submitted by $author";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $from . "\r\n" .
'Reply-To: ' . $from . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//Send the e-mail
mail($to, $subject, $content, $headers);
}
为了隐私,我删除了一些东西,但不应该讨论这个问题。一如既往,非常感谢任何想法!
答案 0 :(得分:7)
由于pending_email
不是全局函数并且是控制器上的方法,因此您需要调用它:
$this->pending_email($blog_id);
$this
关键字是指博客文章控制器类。
答案 1 :(得分:6)
由于这些方法位于您的控制器类中,您应该使用$this->pending_email()
,而不仅仅是pending_email()
:
$this->pending_email($blog_id);
答案 2 :(得分:1)
你在一个物体中,你需要用$ this来引用它。
$this->pending_email($blog_id);
答案 3 :(得分:1)
我想你应该使用:
$this->pending_email($blog_id);
它会起作用。