【发布时间】:2014-08-13 12:05:18
【问题描述】:
Cakephp 新手,我有点挣扎,不知道你能不能帮忙...
所以我按照 CakePHP 博客教程完成了它。制作博客后,控制器名为 PostsController.php,模型为 Post.php,视图位于 Posts/index.ctp。
我会提供代码(不是完整的,只是需要解释的相关事件)
Controller/PostsController.php如下:
<?php
class PostsController extends AppController {
public $helpers = array('Html', 'Form');
public function index() {
$this->set('posts', $this->Post->find('all'));
$this->set('title_for_layout', 'forums');
}
public function add() {
if ($this->request->is('post')) {
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add your post.'));
}
}
}
?>
查看/Posts/index.ctp如下:
<?php echo "<h1>" . $this->Html->link('Add Post', array('action' => 'add')); ?>
<table style="float: left; margin-left: 100px; display: none; " id="hidethis">
<!-- Here's where we loop through our $posts array, printing out post info -->
<?php foreach ($posts as $post): ?>
<tr>
<td width="22%" style=" font-size: .75em; text-align: left;">
<?php echo $post['Post']['created']; ?>
</td>
<td width="78%">
<?php
echo
$post['Post']['title']
;
?>
</td>
</tr>
<?php endforeach; ?>
</table>
Model/Post.php如下:
<?php
class Post extends AppModel {
public $validate = array(
'title' => array(
'rule' => 'notEmpty'
),
'body' => array(
'rule' => 'notEmpty'
)
);
}
?>
所以这很好用,但是我想要一个有 3 个主页的网站;主页、关于我们、联系我们。
现在在关于我们的页面上,我将有内容和一个部分,我想成为我创建的帖子,如上面的示例所示。但是要做到这一点,我希望能够拥有:Controller/AboutsController.php、Model/About.php view/Abouts/about.ctp 以及需要/包含整个 Posts MVC 的类型。
如果我只是将我的 about us 页面设为 Posts MVC,那么名称就不会是我真正想要的名称,这意味着由于 cakephp 命名约定,这些页面将被称为帖子而不是 about。
我希望这是有道理的,有人可以帮忙吗?
【问题讨论】:
标签: cakephp