0

我是 cakephp 新手。我刚刚test使用下面的行在 cake php 中创建了一个项目

php composer.phar create-project --prefer-dist cakephp/app test

之后我在netbeans中导入这个项目

我更改了数据库配置config/app.php(只是更改了用户名和密码)

当我运行该项目时,一切都很好,并且 cakephp 页面在那里。

现在我按照互联网上的教程创建了三个文件

src/控制器/PostsController.php

<?php

class PostsController extends \App\Controller\AppController {
   public $helpers = array('Html', 'Form');



    public function index() {
         $this->set('posts', $this->Post->find('all'));
    }

    public function view($id = null) {
        if (!$id) {
            throw new NotFoundException(__('Invalid post'));
        }

        $post = $this->Post->findById($id);
        if (!$post) {
            throw new NotFoundException(__('Invalid post'));
        }
        $this->set('post', $post);
    }
}

?>

src/Model/Post.php

<?php

class Post extends AppModel {

}

?>

src/View/Posts/index.ctp

<html>
<body>
<h1>Blog posts</h1>

<?php foreach ($posts as $post): ?>
<p><?php echo $post['Post']['title']; ?> | <?php echo $post['Post']['created']; ?>

<?php endforeach; ?>
<?php unset($post); ?>
    </body>

 </html>

我也改变了2行config/routes.php

 $routes->connect('/', ['controller' => 'Posts', 'action' => 'index', 'home']);

/**
 * ...and connect the rest of 'Pages' controller's URLs.
 */
$routes->connect('/posts/*', ['controller' => 'Posts', 'action' => 'index']);

当我转到 url http://localhost:8888/test/posthttp://localhost:8888/testhttp://localhost:8888/test/posts

我总是得到错误

Error: PostController could not be found.
Error: Create the class PostController below in file: src/Controller/PostController.php
4

2 回答 2

0

在 PostsController.php 中进行以下更改:

namespace App\Controller;
use App\Controller\AppController;

class PostsController extends AppController
{

    public function index()
    {
        $this->set('posts', $this->paginate($this->Posts));
        $this->set('_serialize', ['posts']);
    }

  [...]

}

在您的网址中尝试:http://localhost:8888/test/posts

于 2015-09-08T15:23:26.710 回答
-1

在控制器中进行以下更改:

class PostsController extends AppController {

}
于 2015-06-17T10:52:32.123 回答