【问题标题】:PostsController::index() was not found - CakePHP找不到 PostsController::index() - CakePHP
【发布时间】:2014-01-20 05:23:16
【问题描述】:

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

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>

我还加载了本地化和调试工具包插件。我只是将精确的文件夹放入 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 中附上了它的快照

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

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 的输出快照:

【问题讨论】:

  • 好问题。 “我仍然认为值得一提” - 是的,如果您正在编写教程,读者不希望必须访问教程来了解您正在编写的内容。正如您所做的那样,在您的帖子中提供所有内容。 +1

标签: php apache cakephp wamp autoload


【解决方案1】:

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

每个控制器都有自己的目录和视图文件。

【讨论】:

  • 非常感谢。我想我以某种方式误读了教程。我现在觉得有点尴尬。但是,如果您正在学习新东西,问一些简单的问题并不羞耻。
【解决方案2】:

视图文件存储在 /app/View/ 的文件夹中,该文件夹以使用文件的控制器命名,并以其对应的操作命名。在您的示例中,Post 控制器的“index()”操作的视图文件通常可以在 /app/View/Posts/index.ctp 中找到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    相关资源
    最近更新 更多