我想知道为什么不会生成$ Articles

时间:2019-06-11 03:58:58

标签: php cakephp

在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,
    ];
}

我复制了所有这些内容,对这篇文章进行了很多搜索,但找不到 文章来自哪里。 请教我> << / p>

1 个答案:

答案 0 :(得分:0)

我认为

class ArticleController extends AppController

应该是

class ArticlesController extends AppController

我认为文件名也应该是ArticlesController.php。