CakePHP博客不显示任何东西
我试图赶上CakePHP的速度。我之前使用过MVC模式,并且很熟悉这个想法。我试图按照CakePHP的2. *版本的博客教程,并没有运气。CakePHP博客不显示任何东西
如果我浏览到http://localhost/posts/index
,我看到:
未找到
所请求的URL /帖子在此服务器上找到。
这一切看起来很好,如果我只是加载http://localhost/
其他的事情,我不明白的是控制器是如何调用: $this->Post->find(’all’));
没有在Post模型称为find
方法。该模型是完全裸露的:
class Post extends AppModel {
}
我不知道该怎么做。框架是否生成了一个查找方法或者已经将该教程的编写省略了一个非常重要的部分呢?
编辑 - 详情 有在文件夹应用程序的控制器/控制器称为PostsController:
class PostsController extends AppController {
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>
该模型在上面的原始文章中列出。
在数据库中,有我在本教程中使用了以下数据:
/* First, create our posts table: */
CREATE TABLE posts (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50),
body TEXT,
created DATETIME DEFAULT NULL,
modified DATETIME DEFAULT NULL
);
/* Then insert some posts for testing: */
INSERT INTO posts (title,body,created)
VALUES (’The title’, ’This is the post body.’, NOW());
INSERT INTO posts (title,body,created)
VALUES (’A title once again’, ’And the post body follows.’, NOW());
INSERT INTO posts (title,body,created)
VALUES (’Title strikes back’, ’This is really exciting! Not.’, NOW());
我不知道做的什么。框架是否会生成一个查找方法,或者是否已经写了本教程而忽略了它的一个非常重要的部分?
是的,框架照顾的ORM部分..我想你是超'新'这个..即使我是cakephp新...我只有5个项目旧CakePHP,所以即使我新...
好吧......
回到你的问题:
你需要有一个“后”控制器和“索引”的行动。
确保你 '使用' 模式一样,你也可以从一个动作调用它像这样:
$this->loadModel('Post');
$this->set($variable, $this->Post->find('all'));
,然后在你的意见
做:
<?php pr($variable) ?>
需要的不是'短期'的鱼,而是自己钓鱼的能力......我上面给出的例子会让你对CakePHP的工作有所了解。
有问题? :)
编辑:你有MOD重写的问题,就是这样!
这样做:
打开app/Config/core.php
找到行并取消它:从所有文档根目录,应用DIF,webroot的目录
Configure::write('App.baseUrl', env('SCRIPT_NAME'));
全部删除,...的.htaccess
解决?
这是真的。我是0个老项目。我确实有一个控制器和模型,因为我一直在关注“创建博客”教程。所以,我的问题是,为什么它不起作用?教程是否过时? – onefootswill 2013-03-11 04:12:31
我不确定它是否过时。你可以复制粘贴你完成的'完整'作品吗?这将帮助我了解哪里,哪里出了问题......啊!另外,你有数据库中的演示数据? – Karma 2013-03-11 04:58:28
我编辑了这篇文章并添加了我的全部工作。这里没有太多,因为我没有深入了解,因为它没有立即失败。 – onefootswill 2013-03-11 05:31:41
第一个问题听起来像是mod_rewrite问题,请检查食谱中的URL rewriting章节。
该框架是否生成一个find方法,或者是否已经写了本教程省略了它的一个非常重要的部分?
否和否。这是简单的PHP functionality,你只需要遵循继承层次结构来找到find
方法来自哪里:Post
延伸AppModel
延伸Model
。如果您检查API,您会看到Model
定义了您的Post
模型继承的find
方法。
是的。我明白你的观点。在你提到它之后,花了我一段时间找到那个文件。网址重写与我的WAMP设置完全不同。对我来说这是双重荷兰人。我怀疑这是问题所在,我将不得不燃烧一小时的卡车。感谢您在正确的方向推动。 – onefootswill 2013-03-11 06:29:31
从Config/core.php中设置高于0的调试级别,然后查看http:// localhost/posts/index显示的内容 – 2013-03-11 04:18:27
@KishorKundan调试级别已在2.它显示Not Found所请求的URL/Posts是没有在这台服务器上找到。 – onefootswill 2013-03-11 04:52:53