提问者:小点点

CakePHP重定向404s


我第一次尝试学习CakePHP(我通常只学习纯代码PHP),我从他们主持的“官方”博客教程开始:http://book.cakephp.org/1.3/en/The-Manual/Tutorials-Examples/Blog.html

到目前为止,我已经安装了一个虚拟主机(在OS X 10.8.2上),并使用CakePHP的默认索引页来确保它正确地从数据库中读取,应用/tmp文件夹可递归写入Apache等。

在设置了初始博文视图之后,当我尝试关注博文时遇到了问题,它说您现在可以在(将“www.example.com”调整为我的本地服务器名)“cakeblog/Posts/index”上查看博文。我很确定我有一些修改的问题,但我不知道是什么。无论何时发生这种情况,我的Apache错误日志如下:

[Thu Feb 14 09:18:10 2013] [error] [client 127.0.0.1] File does not exist: /Users/bailey/Sites/cakeblog/posts

[Thu Feb 14 09:18:10 2013] [error] [client 127.0.0.1] File does not exist: /Users/bailey/Sites/cakeblog/favicon.ico

我知道favicon存在于我的webroot文件夹中,位于/Users/bailey/Sites/cascade/extranet-cake/app/webroot。如果我理解路由权,那么cakeblog/posts/index应该是PostsController的控制器,/index应该是PostsController“index()”中的操作/方法。所以它似乎不认识控制器?

我在博客教程中设置的代码是:

(app/Model/Post.php):

<?php

    /*
        Model: represents a data model (object).
            Examples -> a blog, a post, a comment on a post
    */
    class Post extends AppModel
    {

    }

?>

(app/Controller/PostsController.php):

<?php
    /*
        Plays with Posts Model and gets work done.

        - function "foo()" means that the function is accessed by
            going to DOMAIN/posts/foo
    */
    class PostsController extends AppController
    {
        public $helpers = array('Html', 'Form');

        // An action!
            // www.example.com/posts/index => listing of all posts
        /*
            Sets the view variable called ‘posts’ equal to the return 
            value of the find('all') method of the Post model.
        */
        public function index()
        {
            $this->set('posts', $this->Post->find('all'));
        }
    }

?>

(app/查看/帖子/index.ctp):

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

    <h2> Blog Posts </h2>
    <table>
        <tr>
            <th> ID </th>
            <th> Title </th>
            <th> Date Created </th>
        </tr>
        <!-- Output the actual posts -->
        <?php
            foreach ($posts as $post)
            {
                /* Data ~ $post[ModelName][VariableName] */
                $id = $post['Post']['id'];
                /*
                    "$this->Html" ~ a Helper
                        link() generates an HTML link with given title and URL
                */
                $titleLink = 
                    $this->Html->link($post['Post']['title'],
                                        array('controller' => 'posts', 'action' => 'view', $post['Post']['id']));

                $dateCreated = $post['Post']['created'];


                $out = "<tr>
                            <td>$id</td>
                            <td>
                                $titleLink
                            </td>
                            <td>$dateCreated</td>
                        </tr>";

                echo $out;
            }

            unset($post);
        ?>
    </table>

这是我第一次真正了解CakePHP,我在其他任何帖子中都找不到解决方案,除了怀疑mod_重写问题的间接原因。有人知道我遗漏了什么吗?我可以发布我的httpd。conf文件,也可以根据请求。


共1个答案

匿名用户

[已解决]

愚蠢的错误——原来我使用了app/webroot以外的子文件夹作为webroot。一旦我改变了这个,它工作得很好。

如果我想使用一个不同于默认的webroot,一个替代方案是由杰里米哈里斯在上面的评论中给出的,并在CakePHP文档中得到支持:

http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Installation.html

谢谢大家的帮助!