【问题标题】:I want to know why $Articles will not be generate我想知道为什么不会生成 $Articles
【发布时间】:2019-10-25 10:29:40
【问题描述】:

在CakePHP教程中,我想做文章,但我不知道。

我正在处理cakePHP,沿着这个,错误显示Call to a member function find() on boolean

这是 ArticleController

<?php
namespace App\Controller;
use App\Controller\AppController;

class ArticleController extends AppController {
    public function index() {
        $this->loadComponent('Paginator');
        debug($this->Articles);
        $articles = $this->Paginator->paginate($this->Articles->find());
        $this->set(compact('articles'));
    }
    public function edit() {
        $article = $this->Articles->findBySlug($slug)->firstOrFail();
        if ($this->request->is(['post', 'put'])) {
            $this->Articles->patchEntity($article, $this->request->getData());
            if ($this->Articles->save($article)) {
                $this->Flash->success(__('Your article has been updated.'));
                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('Unable to update your article.'));
        }
        $this->set('article', $article);
    }
    public function view($slug = null)
    {
        $article = $this->Articles->findBySlug($slug)->firstOrFail();
        $this->set(compact('article'));
    }

}

这是文章索引

<!-- File: src/Template/Articles/index.ctp -->

<h1>Articles</h1>
<table>
    <tr>
        <th>Title</th>
        <th>Created</th>
    </tr>

    <!-- Here is where we iterate through our $articles query object, printing out article info -->

    <?php foreach ($articles as $article): ?>
    <tr>
        <td>
            <?= $this->Html->link($article->title, ['action' => 'view', $article->slug]) ?>
        </td>
        <td>
            <?= $article->created->format(DATE_RFC850) ?>
        </td>
    </tr>
    <?php endforeach; ?>
</table>

这是模型。

<?php
// src/Model/Entity/Article.php
namespace App\Model\Entity;

use Cake\ORM\Entity;

class Article extends Entity
{
    protected $_accessible = [
        '*' => true,
        'id' => false,
        'slug' => false,
    ];
}

我把所有的都复制了,搜索了很多这篇文章,但找不到 文章的来源。 请教我>

【问题讨论】:

  • 你的类应该叫ArticlesController,而不是ArticleController,然后自动魔法就会发生。
  • 谢谢!我找到了
  • 您需要在控制器上加载您的文章模型。
  • 这可能是由一个简单的印刷错误引起的,但这是一个不明显的问题。我暂时投票结束,但这个问题可能对其他人有用——我不熟悉这个框架。

标签: php cakephp


【解决方案1】:

我觉得

class ArticleController extends AppController

应该是

class ArticlesController extends AppController

另外,在我看来,文件名应该是 ArticlesController.php。

【讨论】:

    猜你喜欢
    • 2015-08-06
    • 1970-01-01
    • 2021-09-06
    • 2014-01-28
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多