cakephp_CakePart1.pdf

(467 KB) Pobierz
Microsoft Word - CakePHP Part 1.doc
CakePHP Part 1 – Baking a Cake
Getting Started
Through this tutorial, we are going to create a webapp. Since I’m a youth minister, we
are going to create an event management app that lets me keep track of the students in my
youth group, events we have planned, and which students have signed up for each event.
We’ll try to do this in such a way that it is easily extendable and maintainable. We will
call it ekklesia (Greek for ‘church’).
Downloading and Installing Cake
Cake’s official website is www.cakephp.org . You can get the stable (v. 1.1.12.4205 as of
this writing) or the development (v. 1.2.0.4206) version from there. To use Cake, you
need to have a web server and a database server already installed, configured, and
running. If you don’t have one, I recommend XAMPP from www.apachefriends.org . It
will install PHP4/PHP5, Apache, and MySQL for you. While you’re at it, add php.exe to
your system path. This will make things go more smoothly later.
Once you have your server up and running, download Cake (I’m using the development
version). By design, you should extract Cake somewhere, and then point your webroot to
cake/app. That’s the standard way to do things, but I don’t like doing things that way.
Instead, I have my webroot pointing to c:\web\projects. I extracted cake into my
webroot, so cake lives at c:\web\projects\cake.
Also, by default, the app directory inside cake is where your application files should go.
Again, I don’t like doing things that way. I want to have one cake install that services all
of my web apps. I don’t want to have to have a separate cake folder for each app I write.
For this tutorial, we are going to make a new app that will live at
c:\web\projects\ekklesia. This isn’t the standard way to do things, but it’s not hard to
make it work this way, and it fits how I work better.
Creating Our Database
I’m using MySQL, so I created a database called ekklesia using the community version
of SQLyog ( www.webyog.com ). Here is the structure for my tables (the picture was
created in DBDesigner):
Please note – DBDesigner, while it creates very nice visual representations of a database,
does have some problems – it won’t let me set the length for my VARCHAR’s. For this
example, I set them all to 50. Here is a tweaked script for creating all the tables:
CREATE TABLE events (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
event_name VARCHAR(50) NULL,
event_start DATETIME NULL,
event_end DATETIME NULL,
event_description TEXT NULL,
event_cost FLOAT NULL,
event_deposit FLOAT NULL,
event_deposit_due DATE NULL,
PRIMARY KEY(id)
);
CREATE TABLE registrations (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INTEGER UNSIGNED NULL,
event_id INTEGER UNSIGNED NULL,
permission_slip BOOL NULL,
paid FLOAT NULL,
balance FLOAT NULL,
PRIMARY KEY(id, events_id, users_id),
);
332049262.001.png
CREATE TABLE schools (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
school_name VARCHAR(50) NULL,
PRIMARY KEY(id, users_id),
);
CREATE TABLE users (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NULL,
address1 VARCHAR(50) NULL,
address2 VARCHAR(50) NULL,
city VARCHAR(50) NULL,
state VARCHAR(50) NULL,
zip VARCHAR(50) NULL,
home_phone VARCHAR(50) NULL,
cell_phone VARCHAR(50) NULL,
birthday DATE NULL,
school_id INTEGER UNSIGNED NULL,
school_grade INTEGER UNSIGNED NULL,
email_address VARCHAR(50) NULL,
myspace_name VARCHAR(50) NULL,
PRIMARY KEY(id)
);
Baking a Cake App
Alright, we have everything installed, and our database is ready. Let’s bake.
Baking Models
Bake is a Cake script that creates Cake projects, models, views, and controllers. We’ll
start by baking a project. Open a command prompt and go to your cake scripts directory.
For me, that’s c:\web\projects\cake\cake\scripts. Now type the following:
php bake.php –project c:\web\projects\ekklesia
Hit enter twice, and your basic project is created. (Note: This always gives me errors, but
the don’t seem to affect anything.) We now have a folder called ekklesia with a basic
cake project inside it. We do need to do a little editing before we go further (remember,
I’m using the development version of cake). Go to c:\web\projects\ekklesia (or wherever
you are baking this to). Create a folder called models, and another called temp. Inside
temp, create a folder called cache. Inside cache, create a folder called models.
Now we will use bake to configure our database. At the command prompt, type:
php bake.php –app c:\web\projects\ekklesia
Enter your database type (mysql), database server (localhost is default), database
username (root for this example), database password, ekklesia for the database name,
table prefix (n for this example), and hit enter to tell bake it looks OK.
Bake will now ask you what you would like to bake:
332049262.002.png
We will begin by baking our models, so type M and hit Enter.
Type 1 for the event table and hit Enter. Type yes to set validation criteria. Take all the
defaults. Tell it no, we don’t want to define model associations. I prefer to do that
myself. Double-check that everything looks OK, and then tell it yes.
Next, it is going to try to bake unit test files. I always tell it yes, but I’ll leave that up to
you.
Congratulation! You just baked your first model. Now you are back at the command
prompt. Hit the UP Arrow and Enter to bake another model. Bake models for all of your
tables.
I’ll wait . . .
Baking Controllers
Now let’s bake some controllers. Again, hit the UP Arrow or type:
php bake.php –app c:\web\projects\ekklesia
First, we’ll bake the events controller. Let’s do it interactively (just for fun). We aren’t
going to use any other models, helpers, or components. We will use sessions. We don’t
want to include basic methods right now, but we do want to use scaffolding. It looks OK.
Again, the unit tests are up to you. Now bake your other controllers. I’ll wait . . .
332049262.003.png
Zgłoś jeśli naruszono regulamin