working through cake blog tutorial
This commit is contained in:
parent
d65971eb9c
commit
d5889c0cb8
@ -26,8 +26,11 @@
|
|||||||
* its action called 'display', and we pass a param to select the view file
|
* its action called 'display', and we pass a param to select the view file
|
||||||
* to use (in this case, /app/views/pages/home.ctp)...
|
* to use (in this case, /app/views/pages/home.ctp)...
|
||||||
*/
|
*/
|
||||||
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
|
// Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
|
||||||
|
Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
|
||||||
/**
|
/**
|
||||||
* ...and connect the rest of 'Pages' controller's urls.
|
* ...and connect the rest of 'Pages' controller's urls.
|
||||||
*/
|
*/
|
||||||
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
|
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
|
||||||
|
|
||||||
|
?>
|
||||||
|
47
web-cake/html/app/controllers/posts_controller.php
Normal file
47
web-cake/html/app/controllers/posts_controller.php
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class PostsController extends AppController {
|
||||||
|
public $helpers = array("Html", "Form");
|
||||||
|
public $name = "Posts";
|
||||||
|
|
||||||
|
function index() {
|
||||||
|
$this->set("posts", $this->Post->find("all"));
|
||||||
|
}
|
||||||
|
|
||||||
|
function view($id = null) {
|
||||||
|
$this->Post->id = $id;
|
||||||
|
$this->set("post", $this->Post->read());
|
||||||
|
}
|
||||||
|
|
||||||
|
function add() {
|
||||||
|
if (!empty($this->data)) {
|
||||||
|
if ($this->Post->save($this->data)) {
|
||||||
|
$this->Session->setFlash("Your post has been saved.");
|
||||||
|
$this->redirect(array("action" => "index"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function delete($id) {
|
||||||
|
if ($this->Post->delete($id)) {
|
||||||
|
$this->Session->setFlash("The post with id: " . $id . " has been deleted");
|
||||||
|
$this->redirect(array("action" => "index"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function edit($id = null) {
|
||||||
|
$this->Post->id = $id;
|
||||||
|
if (empty($this->data)) {
|
||||||
|
$this->data = $this->Post->read();
|
||||||
|
} else {
|
||||||
|
if ($this->Post->save($this->data)) {
|
||||||
|
$this->Session->setFlash("Your post has been updated");
|
||||||
|
$this->redirect(array("action" => "index"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
?>
|
15
web-cake/html/app/models/post.php
Normal file
15
web-cake/html/app/models/post.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class Post extends AppModel {
|
||||||
|
public $name = "Post";
|
||||||
|
public $validate = array(
|
||||||
|
"title" => array(
|
||||||
|
"rule" => "notEmpty"
|
||||||
|
),
|
||||||
|
"body" => array(
|
||||||
|
"rule" => "notEmpty"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
7
web-cake/html/app/views/posts/add.ctp
Normal file
7
web-cake/html/app/views/posts/add.ctp
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<h1>Add Post</h1>
|
||||||
|
<?php
|
||||||
|
echo $this->Form->create("Post");
|
||||||
|
echo $this->Form->input("title");
|
||||||
|
echo $this->Form->input("body", array("rows" => "3"));
|
||||||
|
echo $this->Form->end("Save Post");
|
||||||
|
?>
|
8
web-cake/html/app/views/posts/edit.ctp
Normal file
8
web-cake/html/app/views/posts/edit.ctp
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<h1>Edit Post</h1>
|
||||||
|
<?php
|
||||||
|
echo $this->Form->create("Post", array("action" => "edit"));
|
||||||
|
echo $this->Form->input("title");
|
||||||
|
echo $this->Form->input("body", array("rows" => "3"));
|
||||||
|
echo $this->Form->input("id", array("type" => "hidden"));
|
||||||
|
echo $this->Form->end("Save Post");
|
||||||
|
?>
|
40
web-cake/html/app/views/posts/index.ctp
Normal file
40
web-cake/html/app/views/posts/index.ctp
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<h1>Blog posts</h1>
|
||||||
|
<?php echo $this->Html->link(
|
||||||
|
"Add Post", array("controller" => "posts", "action" => "add")
|
||||||
|
);
|
||||||
|
?>
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<th>Id</th>
|
||||||
|
<th>Title</th>
|
||||||
|
<th>Actions</th>
|
||||||
|
<th>Created</th>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<?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 $this->Html->link(
|
||||||
|
"Delete", array("action" => "delete", $post["Post"]["id"]),
|
||||||
|
null, "Are you sure?"
|
||||||
|
);
|
||||||
|
?>
|
||||||
|
<?php echo $this->Html->link(
|
||||||
|
"Edit", array("action" => "edit", $post["Post"]["id"])
|
||||||
|
);
|
||||||
|
?>
|
||||||
|
</td>
|
||||||
|
<td><?php echo $post['Post']['created']; ?></td>
|
||||||
|
</tr>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
|
||||||
|
</table>
|
3
web-cake/html/app/views/posts/view.ctp
Normal file
3
web-cake/html/app/views/posts/view.ctp
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<h1><?php echo $post['Post']['title']?></h1>
|
||||||
|
<p><small>Created: <?php echo $post['Post']['created']?></small></p>
|
||||||
|
<p><?php echo $post['Post']['body']?></p>
|
Loading…
Reference in New Issue
Block a user