Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
找不到PostsController::index()-CakePHP_Php_Apache_Cakephp_Wamp_Autoload - Fatal编程技术网

找不到PostsController::index()-CakePHP

找不到PostsController::index()-CakePHP,php,apache,cakephp,wamp,autoload,Php,Apache,Cakephp,Wamp,Autoload,我正在努力掌握cakephp。所以我从cakephp演示博客应用程序开始。我遵循了教程的前几个步骤,当我尝试加载posts控制器时,它会说 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 我

我正在努力掌握cakephp。所以我从cakephp演示博客应用程序开始。我遵循了教程的前几个步骤,当我尝试加载posts控制器时,它会说

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论坛甚至Google群组中搜索了很多关于这一点的信息,但似乎没有一个解决方案对我有效。发布的大多数解决方案如下所示:

  • 检查mod_rewrite是否已启用——是,我已启用它

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

  • 使用标签而不是php短标签——我使用的是传统的标签

  • 开发环境

    Web服务器-WAMP 我创建了一个名为
    cakeblog
    的别名,并将其指向
    cakephp\u文件夹/app/webroot/

    cakeblog.conf

    core.php

       <?php
        class Post extends AppModel {
            //put your code here
        }
        ?>
    
    <?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);
        }
    }
    
    ?>
    
    <!-- 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>
    
    我能够启动该应用程序,我看到欢迎页面很好。我在startup.png中附加了它的快照

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

    app/model/post.php

       <?php
        class Post extends AppModel {
            //put your code here
        }
        ?>
    
    <?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);
        }
    }
    
    ?>
    
    <!-- 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>
    
    
    
    app/controller/PostsController.php

       <?php
        class Post extends AppModel {
            //put your code here
        }
        ?>
    
    <?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);
        }
    }
    
    ?>
    
    <!-- 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>
    
    
    
    app/view/page/Posts/index.ctp

       <?php
        class Post extends AppModel {
            //put your code here
        }
        ?>
    
    <?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);
        }
    }
    
    ?>
    
    <!-- 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>
    
    
    博客帖子
    身份证件
    标题
    创建
    
    请检查attachment output.png以获取我正在接收的
    http://localhost/cakeblog/posts


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


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

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

    非常感谢。我想我一定是误读了教程。我现在觉得有点尴尬。但是,如果你正在学习新的东西,问一些简单的问题并不羞耻。好问题。“我仍然认为这值得一提”-是的,如果你正在编写教程,读者不希望必须访问教程才能了解你正在编写的内容。在你的岗位上提供一切,就像你所做的那样+1.