【问题标题】:Cakephp Blog Tutorial errorCakephp博客教程错误
【发布时间】:2013-10-31 15:17:03
【问题描述】:

我是 cakephp 新手。我遵循了这个http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html 教程,当我单击编辑链接时,除了“编辑方法”之外,一切都工作正常,它会给出以下警告消息并且不进行编辑。

Warning (2): strtolower() expects parameter 1 to be string, array given [CORE/Cake/Network/CakeRequest.php, line 471]
Code Context
$type = array(
    (int) 0 => 'post',
    (int) 1 => 'put'
)
$this = object(CakeRequest) {
    params => array(
        'plugin' => null,
        'controller' => 'posts',
        'action' => 'edit',
        'named' => array([maximum depth reached]),
        'pass' => array(
            [maximum depth reached]
        )
    )
    data => array()
    query => array()
    url => 'posts/edit/2'
    base => '/cake_2_0'
    webroot => '/cake_2_0/'
    here => '/cake_2_0/posts/edit/2'
}
strtolower - [internal], line ??
CakeRequest::is() - CORE/Cake/Network/CakeRequest.php, line 471
PostsController::edit() - APP/Controller/PostsController.php, line 48
ReflectionMethod::invokeArgs() - [internal], line ??
Controller::invokeAction() - CORE/Cake/Controller/Controller.php, line 485
Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 186
Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 161
[main] - 

【问题讨论】:

  • 张贴一些来自PostsController.php 的代码应该在function edit()
  • 公共函数编辑($id = null) { if (!$id) { throw new NotFoundException(__('Invalid post')); } $post = $this->Post->findById($id); if (!$post) { throw new NotFoundException(__('Invalid post')); } if ($this->request->is(array('post', 'put'))) { $this->Post->id = $id; if ($this->Post->save($this->request->data)) { $this->Session->setFlash(__('你的帖子已更新')); return $this->redirect(array('action' => 'index')); }
  • $this->Session->setFlash(__('无法更新您的帖子。')); } if (!$this->request->data) { $this->request->data = $post; } }
  • 代码应该出现在问题中!顺便说一句,请始终提及您的确切 CakePHP 版本! (提示:问题与您使用的版本有关)
  • 感谢它确实与版本有关的帮助。它实际上解决了我的问题。我正在使用 2.2 版本并遵循教程 2.4!

标签: cakephp


【解决方案1】:

为了进一步澄清这一点,它是

$this->request->is(array('post', 'put'))

导致问题的调用,因为 CakeRequest::is() 在 CakePHP 2.4.0 中只能使用 array,早期版本需要 string

我不确定是否应该期望这些教程与旧版本兼容,但是,为了完整起见,在旧版本中,您必须多次调用 CakeRequest::is()

$this->request->is('post') || $this->request->is('put')

另见

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多