【发布时间】:2013-11-15 18:07:52
【问题描述】:
我是一名 CakePHP 初学者,我想我一定缺少一些基本的基础知识。我要做的是在我的视图文件中有一个链接,单击该链接会导致执行模型方法。我正在尝试使用 CakePHP 博客教程作为起点。
我的视图文件 (Sites/view.ctp) 包括以下链接:
<tr><td class="label">Actions</td><td><?php echo $this->Form->postLink('Sync Pages', array('action' => 'sync', $site['Site']['id']), array('confirm' => 'Are you sure you want to try that?')); ?> | <?php echo $this->Html->link('Edit', array('action' => 'edit', $site['Site']['id'])); ?></td></tr>
我的控制器文件(SitesController.php)包含以下功能:
// Delete function copied from the Cake tutorial
public function delete($id) {
if ($this->request->is('get')) {
throw new MethodNotAllowedException();
}
if ($this->Site->delete($id)) {
$this->Session->setFlash(__('The site with id: %s has been deleted.', h($id)));
return $this->redirect(array('action' => 'index'));
}
}
// My failing function (results in "Missing View" error)
public function sync($id) {
if ($this->request->is('get')) {
throw new MethodNotAllowedException();
}
if ($this->Site->syncPages($id)) {
$this->Session->setFlash(__('The site with id: %s has been synced.', h($id)));
return $this->redirect(array('action' => 'view', $id));
}
}
我的模型文件(Site.php)包含以下函数草稿:
public function syncPages() {
$cms = $this->cms;
if ($this->cms == 'Wordpress')
{
// ... do stuff to retrieve data from a remote Wordpress database and save it to the local CakePHP app database
}
}
但是,如上所述,我根本无法执行该 syncPages() 函数。当我单击同步链接时,我收到“缺少视图”错误。我哪里错了?提前感谢您的帮助!
【问题讨论】:
-
对于那些通过搜索来到这里的人:在我的情况下,
beforeFilter()导致了这个问题。请参阅stackoverflow.com/questions/16921021/… 了解更多信息。
标签: php templates cakephp cakephp-model