RSS

Search Engine

Showing posts with label Cake Book Blog. Show all posts
Showing posts with label Cake Book Blog. Show all posts

Saturday, May 15, 2010

Deleting Posts

Next, let's make a way for users to delete posts. Start with a delete() action in the PostsController:
Plain Text View

function delete($id) {
$this->Post->delete($id);
$this->Session->setFlash('The post with id: '.$id.' has been deleted.');
$this->redirect(array('action'=>'index'));
}

1. function delete($id) {
2. $this->Post->delete($id);
3. $this->Session->setFlash('The post with id: '.$id.' has been deleted.');
4. $this->redirect(array('action'=>'index'));
5. }

This logic deletes the post specified by $id, and uses $this->Session->setFlash() to show the user a confirmation message after redirecting them on to /posts.

Because we're just executing some logic and redirecting, this action has no view. You might want to update your index view with links that allow users to delete posts, however:
Plain Text View



Blog posts


link('Add Post', array('action' => 'add')); ?>





















Id Title Actions Created

link($post['Post']['title'], array('action' => 'view', 'id' => $post['Post']['id']));?>

link('Delete', array('action' => 'delete', 'id' => $post['Post']['id']), null, 'Are you sure?' )?>


1.
2.

Blog posts


3.

link('Add Post', array('action' => 'add')); ?>


4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
18.
21.
22.
23.
24.
25.
IdTitleActionsCreated

16. link($post['Post']['title'], array('action' => 'view', 'id' => $post['Post']['id']));?>
17.

19. link('Delete', array('action' => 'delete', 'id' => $post['Post']['id']), null, 'Are you sure?' )?>
20.


This view code also uses the HtmlHelper to prompt the user with a JavaScript confirmation dialog before they attempt to delete a post.

Data Validation

Cake goes a long way in taking the monotony out of form input validation. Everyone hates coding up endless forms and their validation routines. CakePHP makes it easier and faster.

To take advantage of the validation features, you'll need to use Cake's FormHelper in your views. The FormHelper is available by default to all views at $form.

Here's our add view:

Plain Text View
	


Add Post


echo $form->create('Post');
echo $form->input('title');
echo $form->input('body', array('rows' => '3'));
echo $form->end('Save Post');
?>

  1. Add Post

  2. echo $form->create('Post');
  3. echo $form->input('title');
  4. echo $form->input('body', array('rows' => '3'));
  5. echo $form->end('Save Post');
  6. ?>

Here, we use the FormHelper to generate the opening tag for an HTML form. Here's the HTML that $form->create() generates:

Plain Text View

  1. <form id="PostAddForm" method="post" action="/posts/add">

If create() is called with no parameters supplied, it assumes you are building a form that submits to the current controller's add() action (or edit() action when id is included in the form data), via POST.

The $form->input() method is used to create form elements of the same name. The first parameter tells CakePHP which field they correspond to, and the second parameter allows you to specify a wide array of options - in this case, the number of rows for the textarea. There's a bit of introspection and automagic here: input() will output different form elements based on the model field specified.

The $form->end() call generates a submit button and ends the form. If a string is supplied as the first parameter to end(), the FormHelper outputs a submit button named accordingly along with the closing form tag. Again, refer to Chapter "Built-in Helpers" for more on helpers.

Now let's go back and update our /app/views/posts/index.ctp view to include a new "Add Post" link. Before the

, add the following line:

Plain Text View
link('Add Post',array('controller' => 'posts', 'action' => 'add'))?>
  1. echo $html->link('Add Post',array('controller' => 'posts', 'action' => 'add'))?>

You may be wondering: how do I tell CakePHP about my validation requirements? Validation rules are defined in the model. Let's look back at our Post model and make a few adjustments:

Plain Text View
class Post extends AppModel

{
var $name = 'Post';

var $validate = array(
'title' => array(
'rule' => 'notEmpty'
),
'body' => array(
'rule' => 'notEmpty'
)
);
}
?>
  1. class Post extends AppModel
  2. {
  3. var $name = 'Post';
  4. var $validate = array(
  5. 'title' => array(
  6. 'rule' => 'notEmpty'
  7. ),
  8. 'body' => array(
  9. 'rule' => 'notEmpty'
  10. )
  11. );
  12. }
  13. ?>

The $validate array tells CakePHP how to validate your data when the save() method is called. Here, I've specified that both the body and title fields must not be empty. CakePHP's validation engine is strong, with a number of pre-built rules (credit card numbers, email addresses, etc.) and flexibility for adding your own validation rules. For more information on that setup, check the Data Validation chapter.

Now that you have your validation rules in place, use the app to try to add a post with an empty title or body to see how it works. Since we've used the input() method of the FormHelper to create our form elements, our validation error messages will be shown automatically.

Adding Posts

Reading from the database and showing us the posts is a great start, but let's allow for the adding of new posts.

First, start by creating an add() action in the PostsController:

Plain Text View
class PostsController extends AppController {

var $name = 'Posts';

function index() {
$this->set('posts', $this->Post->find('all'));
}

function view($id) {
$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'));
}
}
}
}
?>
  1. class PostsController extends AppController {
  2. var $name = 'Posts';
  3. function index() {
  4. $this->set('posts', $this->Post->find('all'));
  5. }
  6. function view($id) {
  7. $this->Post->id = $id;
  8. $this->set('post', $this->Post->read());
  9. }
  10. function add() {
  11. if (!empty($this->data)) {
  12. if ($this->Post->save($this->data)) {
  13. $this->Session->setFlash('Your post has been saved.');
  14. $this->redirect(array('action' => 'index'));
  15. }
  16. }
  17. }
  18. }
  19. ?>

Here's what the add() action does: if the submitted form data isn't empty, try to save the data using the Post model. If for some reason it doesn't save, just render the view. This gives us a chance to show the user validation errors or other warnings.

When a user uses a form to POST data to your application, that information is available in $this->data. You can use the pr() or debug functions to print it out if you want to see what it looks like.

We use the Session component's setFlash() function to set a message to a session variable to be displayed on the page after redirection. In the layout we have $session->flash() which displays the message and clears the corresponding session variable. The controller's redirect function redirects to another URL. The param array('action'=>'index) translates to URL /posts i.e the index action of posts controller. You can refer to Router::url function on the api to see the formats in which you can specify a URL for various cake functions.

Calling the save() method will check for validation errors and abort the save if any occur. We'll discuss how those errors are handled in the following sections.

Creating Post Views

Now that we have our data flowing to our model, and our application logic and flow defined by our controller, let's create a view for the index action we created above.

Cake views are just presentation-flavored fragments that fit inside an application's layout. For most applications they're HTML mixed with PHP, but they may end up as XML, CSV, or even binary data.

Layouts are presentation code that is wrapped around a view, and can be defined and switched between, but for now, let's just use the default.

Remember in the last section how we assigned the 'posts' variable to the view using the set() method? That would hand down data to the view that would look something like this:

// print_r($posts) output:


Array
(
[0] => Array
(
[Post] => Array
(
[id] => 1
[title] => The title
[body] => This is the post body.
[created] => 2008-02-13 18:34:55
[modified] =>
)
)
[1] => Array
(
[Post] => Array
(
[id] => 2
[title] => A title once again
[body] => And the post body follows.
[created] => 2008-02-13 18:34:56
[modified] =>
)
)
[2] => Array
(
[Post] => Array
(
[id] => 3
[title] => Title strikes back
[body] => This is really exciting! Not.
[created] => 2008-02-13 18:34:57
[modified] =>
)
)
)

Cake's view files are stored in /app/views inside a folder named after the controller they correspond to (we'll have to create a folder named 'posts' in this case). To format this post data in a nice table, our view code might look something like this:

Plain Text View



Blog posts



















Id Title Created

link($post['Post']['title'],
array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
  1. Blog posts

  2. foreach ($posts as $post): ?>
  3. endforeach; ?>
  4. IdTitleCreated
    echo $post['Post']['id']; ?>
  5. echo $html->link($post['Post']['title'],
  6. array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
  7. echo $post['Post']['created']; ?>

Hopefully this should look somewhat simple.

You might have noticed the use of an object called $html. This is an instance of the CakePHP HtmlHelper class. CakePHP comes with a set of view helpers that make things like linking, form output, JavaScript and Ajax a snap. You can learn more about how to use them in Chapter "Built-in Helpers", but what's important to note here is that the link() method will generate an HTML link with the given title (the first parameter) and URL (the second parameter).

When specifying URLs in Cake, you simply give a path relative to the base of the application, and Cake fills in the rest. As such, your URLs will typically take the form of /controller/action/param1/param2.

At this point, you should be able to point your browser to http://www.example.com/posts/index. You should see your view, correctly formatted with the title and table listing of the posts.

If you happened to have clicked on one of the links we created in this view (that link a post's title to a URL /posts/view/some_id), you were probably informed by CakePHP that the action hasn't yet been defined. If you were not so informed, either something has gone wrong, or you actually did define it already, in which case you are very sneaky. Otherwise, we'll create it in the PostsController now:

Plain Text View
class PostsController extends AppController {


var $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());
}
}
?>
  1. class PostsController extends AppController {
  2. var $name = 'Posts';
  3. function index() {
  4. $this->set('posts', $this->Post->find('all'));
  5. }
  6. function view($id = null) {
  7. $this->Post->id = $id;
  8. $this->set('post', $this->Post->read());
  9. }
  10. }
  11. ?>

The set() call should look familiar. Notice we're using read() rather than find('all') because we only really want a single post's information.

Notice that our view action takes a parameter: the ID of the post we'd like to see. This parameter is handed to the action through the requested URL. If a user requests /posts/view/3, then the value '3' is passed as $id.

Now let's create the view for our new 'view' action and place it in /app/views/posts/view.ctp.

Plain Text View





Created:



  1. echo $post['Post']['title']?>

  2. Created: echo $post['Post']['created']?>

  3. echo $post['Post']['body']?>

Verify that this is working by trying the links at /posts/index or manually requesting a post by accessing /posts/view/1.

Create a Posts Controller

Next, we'll create a controller for our posts. The controller is where all the business logic for post interaction will happen. In a nutshell, it's the place where you play with the models and get post-related work done. We'll place this new controller in a file called posts_controller.php inside the /app/controllers directory. Here's what the basic controller should look like:

Plain Text View
  

  1. class PostsController extends AppController {
  2. var $name = 'Posts';
  3. }
  4. ?>

Now, lets add an action to our controller. Actions often represent a single function or interface in an application. For example, when users request www.example.com/posts/index (which is also the same as www.example.com/posts/), they might expect to see a listing of posts. The code for that action would look something like this:

Plain Text View
set('posts', $this->Post->find('all'));

}
}
?>

  1. class PostsController extends AppController {
  2. var $name = 'Posts';
  3. function index() {
  4. $this->set('posts', $this->Post->find('all'));
  5. }
  6. }
  7. ?>

Let me explain the action a bit. By defining function index() in our PostsController, users can now access the logic there by requesting www.example.com/posts/index. Similarly, if we were to define a function called foobar(), users would be able to access that at www.example.com/posts/foobar.

You may be tempted to name your controllers and actions a certain way to obtain a certain URL. Resist that temptation. Follow CakePHP conventions (plural controller names, etc.) and create readable, understandable action names. You can map URLs to your code using "routes" covered later on.

The single instruction in the action uses set() to pass data from the controller to the view (which we'll create next). The line sets the view variable called 'posts' equal to the return value of the find('all') method of the Post model. Our Post model is automatically available at $this->Post because we've followed Cake's naming conventions.

To learn more about Cake's controllers, check out Chapter "Developing with CakePHP" section: "Controllers".

Create a Post Model

The Model class is the bread and butter of CakePHP applications. By creating a CakePHP model that will interact with our database, we'll have the foundation in place needed to do our view, add, edit, and delete operations later.

CakePHP's model class files go in /app/models, and the file we'll be creating will be saved to /app/models/post.php. The completed file should look like this:

Plain Text View
  

  1. class Post extends AppModel {
  2. var $name = 'Post';
  3. }
  4. ?>

Naming convention is very important in CakePHP. By naming our model Post, CakePHP can automatically infer that this model will be used in the PostsController, and will be tied to a database table called posts.

CakePHP will dynamically create a model object for you, if it cannot find a corresponding file in /app/models. This also means, that if you accidentally name your file wrong (i.e. Post.php or posts.php) CakePHP will not recognize any of your settings and will use the defaults instead.

The $name variable is always a good idea to add, and is used to overcome some class name oddness in PHP4.

For more on models, such as table prefixes, callbacks, and validation, check out the Models chapter of the Manual.

A Note on mod_rewrite

Occasionally a new user will run in to mod_rewrite issues, so I'll mention them marginally here. If the CakePHP welcome page looks a little funny (no images or css styles), it probably means mod_rewrite isn't functioning on your system. Here are some tips to help get you up and running:

  1. Make sure that an .htaccess override is allowed: in your httpd.conf, you should have a section that defines a section for each Directory on your server. Make sure the AllowOverride is set to All for the correct Directory. For security and performance reasons, do not set AllowOverride to All in . Instead, look for the block that refers to your actual website directory.

  2. Make sure you are editing the correct httpd.conf rather than a user- or site-specific httpd.conf.

  3. For some reason or another, you might have obtained a copy of CakePHP without the needed .htaccess files. This sometimes happens because some operating systems treat files that start with '.' as hidden, and don't copy them. Make sure your copy of CakePHP is from the downloads section of the site or our SVN repository.

  4. Make sure Apache is loading up mod_rewrite correctly! You should see something like LoadModule rewrite_module libexec/httpd/mod_rewrite.so or (for Apache 1.3) AddModule mod_rewrite.c in your httpd.conf.

If you don't want or can't get mod_rewrite (or some other compatible module) up and running on your server, you'll need to use Cake's built in pretty URLs. In /app/config/core.php, uncomment the line that looks like:

Plain Text View
Configure::write('App.baseUrl', env('SCRIPT_NAME'));
  1. Configure::write('App.baseUrl', env('SCRIPT_NAME'));

Also remove these .htaccess files:

Plain Text View
  /.htaccess
/app/.htaccess
/app/webroot/.htaccess
  1. /.htaccess
  2. /app/.htaccess
  3. /app/webroot/.htaccess

This will make your URLs look like www.example.com/index.php/controllername/actionname/param rather than www.example.com/controllername/actionname/param.

Optional Configuration

There are two other items that can be configured. Most developers complete these laundry-list items, but they're not required for this tutorial. One is defining a custom string (or "salt") for use in security hashes. The second item is allowing CakePHP write access to its tmp folder.

The security salt is used for generating hashes. Change the default salt value by editing /app/config/core.php line 153. It doesn't much matter what the new value is, as long as it's not easily guessed.

Plain Text View


  1. /**
  2. * A random string used in security hashing methods.
  3. */
  4. Configure::write('Security.salt', 'pl345e-P45s_7h3*S@l7!');
  5. ?>

The final task is to make the app/tmp directory web-writable. The best way to do this is to find out what user your webserver runs as () and change the ownership of the app/tmp directory to that user. The final command you run (in *nix) might look something like this.

Plain Text View
$ chown -R www-data app/tmp

  1. $ chown -R www-data app/tmp

If for some reason CakePHP can't write to that directory, you'll be informed by a warning while not in production mode.

Cake Database Configuration

Onward and upward: let's tell Cake where our database is and how to connect to it. For many, this is the first and last time you configure anything.

A copy of CakePHP's database configuration file is found in /app/config/database.php.default. Make a copy of this file in the same directory, but name it database.php.

The config file should be pretty straightforward: just replace the values in the $default array with those that apply to your setup. A sample completed configuration array might look something like the following:

Plain Text View
var $default = array(
'driver' => 'mysql',
'persistent' => 'false',
'host' => 'localhost',
'port' => '',
'login' => 'cakeBlog',
'password' => 'c4k3-rUl3Z',
'database' => 'cake_blog_tutorial',
'schema' => '',
'prefix' => '',
'encoding' => ''
);
  1. var $default = array(
  2. 'driver' => 'mysql',
  3. 'persistent' => 'false',
  4. 'host' => 'localhost',
  5. 'port' => '',
  6. 'login' => 'cakeBlog',
  7. 'password' => 'c4k3-rUl3Z',
  8. 'database' => 'cake_blog_tutorial',
  9. 'schema' => '',
  10. 'prefix' => '',
  11. 'encoding' => ''
  12. );

Once you've saved your new database.php file, you should be able to open your browser and see the Cake welcome page. It should also tell you that your database connection file was found, and that Cake can successfully connect to the database.

Creating the Blog Database

Next, lets set up the underlying database for our blog. if you haven't already done so, create an empty database for use in this tutorial, with a name of your choice. Right now, we'll just create a single table to store our posts. We'll also throw in a few posts right now to use for testing purposes. Execute the following SQL statements into your database:

Plain Text View
/* 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());
  1. /* First, create our posts table: */
  2. CREATE TABLE posts (
  3. id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  4. title VARCHAR(50),
  5. body TEXT,
  6. created DATETIME DEFAULT NULL,
  7. modified DATETIME DEFAULT NULL
  8. );
  9. /* Then insert some posts for testing: */
  10. INSERT INTO posts (title,body,created)
  11. VALUES ('The title', 'This is the post body.', NOW());
  12. INSERT INTO posts (title,body,created)
  13. VALUES ('A title once again', 'And the post body follows.', NOW());
  14. INSERT INTO posts (title,body,created)
  15. VALUES ('Title strikes back', 'This is really exciting! Not.', NOW());

The choices on table and column names are not arbitrary. If you follow Cake's database naming conventions, and Cake's class naming conventions (both outlined in "CakePHP Conventions"), you'll be able to take advantage of a lot of free functionality and avoid configuration. Cake is flexible enough to accomodate even the worst legacy database schema, but adhering to convention will save you time.

Check out "CakePHP Conventions" for more information, but suffice it to say that naming our table 'posts' automatically hooks it to our Post model, and having fields called 'modified' and 'created' will be automagically managed by Cake.

Getting Cake

First, let's get a copy of fresh Cake code.

To get a fresh download, visit the CakePHP project on github: http://github.com/cakephp/cakephp/downloads and download the latest 1.2.x.x release.

You can also use git to clone the repository, and checkout the 1.2 branch for the latest development code for this branch.

Regardless of how you downloaded it, place the code inside of your DocumentRoot. Once finished, your directory setup should look something like the following:

Plain Text View
/path_to_document_root
/app
/cake
/docs
/vendors
.htaccess
index.php
  1. /path_to_document_root
  2. /app
  3. /cake
  4. /docs
  5. /vendors
  6. .htaccess
  7. index.php

Now might be a good time to learn a bit about how Cake's directory structure works: check out Chapter "Basic Principles of CakePHP", Section : CakePHP File Structure.