【问题标题】:"Missing View" error when trying to call Model method from Controller in CakePHP尝试从 CakePHP 中的控制器调用模型方法时出现“缺少视图”错误
【发布时间】: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() 函数。当我单击同步链接时,我收到“缺少视图”错误。我哪里错了?提前感谢您的帮助!

【问题讨论】:

标签: php templates cakephp cakephp-model


【解决方案1】:

将 $this->layout = $this->autoRender = false 添加到控制器中的同步函数中。

【讨论】:

  • 这是正确的。我假设您(艾莉森)需要知道为什么它是正确的。 CakePHP 是一个简化的框架,可以让您完成大部分工作。它期望如果您正在处理一个名为“test”的函数,那么一个名为“test.ctp”的视图也存在。它并不关心您是否正在重定向 - 它仍然希望该文件存在。上面 Kai 的代码告诉框架你不会加载布局,也不会像默认情况下那样自动加载视图。
  • 非常感谢,Kai 和 skrilled!似乎 $this-&gt;autoRender = false 单独可以禁用布局和视图(现在我需要阅读两者之间的区别... ;-) 我想这一定是自动渲染关闭的情况默认情况下是内置的“删除”功能,对吧?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多