How to create Blog using CakePHP


How to create Blog using CakePHP

How to create Blog using CakePHP

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

To get a fresh download, visit the CakePHP project at Cakeforge: http://cakeforge.org/projects/cakephp/ and download the stable release. For this tutorial you need 1.2.x.x

You can also checkout/export a fresh copy of our trunk code at: https://svn.cakephp.org/repo/trunk/cake/1.2.x.x/

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.

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());

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.

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’ => ”
);

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.

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

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.

Advertisements

2 thoughts on “How to create Blog using CakePHP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s