我第一次尝试学习 CakePHP(我通常只是直接编写 PHP 代码),我从他们主持的“官方”博客教程开始:http: //book.cakephp.org/1.3/en/The -Manual/Tutorials-Examples/Blog.html
到目前为止,我已经设置了一个虚拟主机(在 OS X 10.8.2 上),并使用 CakePHP 的默认索引页面来确保它正确地从数据库中读取,app/tmp 文件夹对于 Apache 等是递归可写的。
当我在设置初始帖子视图后立即尝试关注博客时遇到问题,它说您现在可以在(将“www.example.com”调整为我的本地服务器名称)“cakeblog/posts/index”查看帖子'。我很确定我遇到了某种 mod_rewrite 问题,但我不知道是什么问题。每当发生这种情况时,我的 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
我知道网站图标存在于 /Users/bailey/Sites/cascade/extranet-cake/app/webroot 的 webroot 文件夹中。如果我理解路由权,cakeblog/posts/index应该是 Post PostsController 的控制器,/index 应该是 PostsController“index()”中的操作/方法。所以它似乎没有识别控制器?
我按照博客教程设置的代码是:
(应用程序/模型/Post.php):
<?php
/*
Model: represents a data model (object).
Examples -> a blog, a post, a comment on a post
*/
class Post extends AppModel
{
}
?>
(应用程序/控制器/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'));
}
}
?>
(应用程序/视图/帖子/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_rewrite 问题的间接原因外,我在其他任何帖子中都找不到解决方案。有人对我缺少的东西有想法吗?我也可以根据要求发布我的 httpd.conf 文件。