简体   繁体   中英

CakePHP Redirect 404s

I'm trying to learn CakePHP for the first time (I usually just straight code PHP), and I'm starting with the 'official' Blog Tutorial they host: http://book.cakephp.org/1.3/en/The-Manual/Tutorials-Examples/Blog.html

So far, I've setup a virtual host (on OS X 10.8.2), and used CakePHP's default index page to make sure it's reading from the database correctly, the app/tmp folder is recursively writeable for Apache, etc.

I run into issues when I try to follow the blog right after the initial Posts View is setup, and it says you can now view the posts at (adjusting 'www.example.com' to my local ServerName) 'cakeblog/posts/index'. I'm pretty sure I'm having some kind of mod_rewrite issue, but I can't figure out what. My Apache error logs for whenever this happens are:

[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

I know the favicon exists in my webroot folder at /Users/bailey/Sites/cascade/extranet-cake/app/webroot. If I'm understanding the routing right, cakeblog/posts/index should be the controller for Post PostsController, and /index should be the action/method in PostsController "index()". So it seems to not be recognizing the Controller?

The code I have setup following the blog tutorial is:

(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/View/Posts/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>

This is the first time I've really seen CakePHP and I can't find solutions in any of the other posts, except circumstantial reasons to suspect a mod_rewrite issue. Anyone have ideas on what I'm missing? I can post my httpd.conf file, too, upon request.

[SOLVED]

Stupid mistake -- it turns out I was using a subfolder other than app/webroot as a webroot. Once I changed this it worked fine.

An alternative if I wanted to use a webroot different from the default was given by JeremyHarris in the comments above and supported in the CakePHP docs:

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

Thanks for the help, everyone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM