首页 > 解决方案 > 我无法解决“缺少控制器错误”

问题描述

我应该制作正确的名称控制器,但缺少控制器错误不会消失。

我正在从https://book.cakephp.org/3.0/en/tutorials-and-examples/cms/articles-controller.html做 cakePHP 教程

它在 /kaede/code/cake/cms/src/Controller

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

class ArticlesController extends AppController {
    public function index() {
        $this->loadComponent('Paginator');
        $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);
    }
}

找不到缺少的控制器 ArticleController。在以下文件中创建 ArticleController 类:src/Controller/ArticleController.php

标签: phpcakephp

解决方案


可以在此处找到该教程的完整源代码。 https://github.com/cakephp/cms-tutorial/blob/master/src/Controller/ArticlesController.php

仔细检查文章和文章的拼写,可能是这种情况。

public function edit($slug)
    {
        $article = $this->Articles
            ->findBySlug($slug)
            ->contain('Tags') // load associated Tags
            ->firstOrFail();
        if ($this->request->is(['post', 'put'])) {
            $this->Articles->patchEntity($article, $this->request->getData(), [
                // Added: Disable modification of user_id.
                'accessibleFields' => ['user_id' => false]
            ]);
            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.'));
        }
        // Get a list of tags.
        $tags = $this->Articles->Tags->find('list');
        // Set article & tags to the view context
        $this->set('tags', $tags);
        $this->set('article', $article);
    }

推荐阅读