php - 未找到 PostsController::index() - CakePHP

标签 php apache cakephp wamp autoload

我正在尝试使用 cakephp。所以我开始使用 cakephp 演示博客应用程序。我按照教程的前几个步骤进行操作,当我尝试加载帖子 Controller 时,它说

Missing View
Error: The view for PostsController::index() was not found.

Error: Confirm you have created the file: T:\Project Folders\NetBeans\cakeBlog\app\View\Posts\index.ctp

我在 stackoverflow 、cakephp 论坛甚至 googlegroups 中都搜索了很多关于这个的内容,但似乎没有一个解决方案对我有用。贴出的大部分解决方案如下:

  1. 检查是否启用了 mod_rewrite -- 是的,我已启用它。

  2. 检查您是否将 index.ctp 命名为 index.ctp 而不是 index.cpt 、 Index.ctp 或任何其他变体。 -- 是的,我已将索引放置如下 app/views/Posts/index.ctp(使用 netbeans 向导)

  3. 使用标签而不是 php 短标签——我使用的是常规标签

开发环境

网络服务器 - WAMP 我创建了一个名为 cakeblog 的别名并将其指向 cakephp_folder/app/webroot/

cakeblog.conf

Alias /cakeblog/ "T:\Project Folders\NetBeans\cakeBlog\app\webroot/" 

<Directory "T:\Project Folders\NetBeans\cakeBlog\app\webroot/">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
        Order allow,deny
    Allow from all
</Directory>

app/webroot/.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /cakeblog
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

我还加载了本地化和 debugkit 插件。我只是将确切的文件夹放入 app/plugin 并在 bootstrap.php 中添加以下内容

bootstrap.php

CakePlugin::load('DebugKit'); //Loads a single plugin named DebugKit
CakePlugin::load('Localized');

core.php

Configure::write('debug', 1);

我能够启动该应用程序,并且可以很好地看到欢迎页面。我在 startup.png 中附上了它的快照

startup

现在,让我粘贴我从教程中复制粘贴的代码:

app/model/post.php

   <?php
    class Post extends AppModel {
        //put your code here
    }
    ?>

app/controller/PostsController.php

<?php

  class PostsController extends AppController {
    //put your code here
    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);
    }
}

?>

app/view/Pages/Posts/index.ctp

<!-- File: /app/View/Posts/index.ctp -->

<h1>Blog posts</h1>
<table>
    <tr>
        <th>Id</th>
        <th>Title</th>
        <th>Created</th>
    </tr>

    <!-- Here is where we loop through our $posts array, printing out post info -->

    <?php foreach ($posts as $post): ?>
    <tr>
        <td><?php echo $post['Post']['id']; ?></td>
        <td>
            <?php echo $this->Html->link($post['Post']['title'],
array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
        </td>
        <td><?php echo $post['Post']['created']; ?></td>
    </tr>
    <?php endforeach; ?>
    <?php unset($post); ?>
</table>

请检查附件 output.png 以获取我收到的 http://localhost/cakeblog/posts 输出快照:

output

最佳答案

将目录 app/View/Pages/Posts/更改为 app/View/Posts/

每个 Controller 都有自己的 View 文件目录。

关于php - 未找到 PostsController::index() - CakePHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20888429/

相关文章:

php - PayPal 单笔付款至电子邮件

apache - 如何通过 http 克隆一个包含 subrepos 的 mercurial repo 而不必对每个 repo 进行身份验证?

apache - 如何下载apache tomcat 6.0.35。

php - Cakephp从多个表中获取数据

php - CodeIgniter get_where order_by

php - 使用 jQuery、PHP 和 MySQL 为单选按钮加载 JSON 数据

php - Ubuntu 14.04 apache2 配置问题

cakephp - Cakephp : Error handling issue (error not inserting in error_log table)

php - CakePHP:如果我在查找中组合 'fields' 和 'contain',我得不到想要的结果。谁能解释为什么?

php - 使用 Carbon PHP 格式化日期