尝试从CakePHP中的Controller调用Model方法时出现“Missing View”错误

时间:2013-11-15 18:07:53

标签: php templates cakephp cakephp-appmodel

我是CakePHP初学者,我想我必须缺少一些必要的基础知识。我要做的是在我的View文件中有一个链接,当单击它时,会导致执行Model方法。我正在尝试使用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()函数。当我点击同步链接时,我收到“缺少视图”错误。我哪里错了?在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

将$ this-&gt; layout = $ this-&gt; autoRender = false添加到控制器中的同步功能。