Anda di halaman 1dari 7

1.

CakePHP Installation

Initializing the prooject directory


create a directory in your home directory mkdir -p project/jobeet
download the framework from https://github.com/cakephp/cakephp/tags
Directory description
on http://book.cakephp.org/2.0/en/getting-started/cakephp-folder-structure.html

2. The Project
The Project user stories
Before diving into the code head-first, let's describe the project a bit more. The following
sections describe the features we want to implement in the first version/iteration of the
project with some simple stories.
The jobeet website has four kind of users:
admin: He owns the website and has the magic power
user: He vists the website to look for a job
poster: He visits the website to post a jo
affilate: He re-publishes some jobs on his website
Story 1: On the homepage, the user sees the latest active jobs
when a user comes to the Jobeet website he sees a list of active jobs. The jobs are sorted by
category and then by publication date.

For each category the list only shows the first 10 jobs and a link allows to list all the jobs for a given
category.

Story 2: A user can ask for all the jobs in a given category
when a user clicks on a category name or on a more jobs link on the homepage, he sees all the
jobs for this category sorted by date. The list is paginated with 20 jobs per page.

Story 3: A user clicks on a job to see more detailed information.


The user can select a job from the list to see more detailed information.

Story 4: A user posts a job

A user can post a job. A job is made of several pieces of information.


Company
Type(full-time,part-time, or freelance)
logo(optional)
url(optional)
Position
location
category(the user chooses in a list of possible categories)
Job description
How to apply
Public
Email
There is no need to create an account to post a job.
The process is straightforward with only two steps: first, the user fills in the form with all the
needed information to describe the job then he validates the information by previewing the final job
page.
Each job post is on line for 30 days (this is configurable by the admin a user can come back to reactivate or extend the validity of the job for an extra 30 days but only when the job expires in less
than 5 days.

3. The Data Model


The user stories we describe is the main objects of our project: jobs, affiliates, and categories. Here
is the corresponding entity relationship diagram:

CREATE TABLE IF NOT EXISTS 'categories' (


'id' int(11) NOT NULL AUTO_INCREMENT,
'name' varchar(255) NOT NULL,
'created' datetime NOT NULL,
'modified' datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `jobs` (


`id` int(11) NOT NULL AUTO_INCREMENT,
`category_id` int(11) NOT NULL,
`type` varchar(255) NOT NULL,
`company` varchar(255) NOT NULL,
`logo` varchar(255) NOT NULL,
`url` varchar(255) NOT NULL,
`position` varchar(255) NOT NULL,
`location` varchar(255) NOT NULL,
`description` text NOT NULL,
`how_to_apply` text NOT NULL,
`token` varchar(255) NOT NULL,
`is_public` tinyint(1) NOT NULL,
`is_activated` tinyint(1) NOT NULL,
`email` varchar(255) NOT NULL,
`expires_at` datetime NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`),
KEY `category_id` (`category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `affiliates` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`url` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`token` varchar(255) NOT NULL,
`is_active` tinyint(1) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `categories_affiliates` (
`category_id` int(11) NOT NULL,
`affiliate_id` int(11) NOT NULL,
PRIMARY KEY (`category_id`,`affiliate_id`),
KEY `affiliate_id` (`affiliate_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `categories_affiliates`
ADD CONSTRAINT `categories_affiliates_ibfk_2` FOREIGN KEY (`affiliate_id`)
REFERENCES `affiliates` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
ADD CONSTRAINT `categories_affiliates_ibfk_1` FOREIGN KEY (`category_id`)
REFERENCES `categories` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE `jobs`
ADD CONSTRAINT `jobs_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `categories`
(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

The database
The CakePHP framework suprots all PDO-supported database (MySQL, PostgreSQL, SQLite,
Oracle, MSSQL, ) for this totorial lets use MySQL for.
$mysqladmin -uroot -p create jobeet
configure your database in app/Config/database.php
See it in Action in the Browser
CakePHP is able to automatically generate a MVC paters for a give model thet provides basic
manipulation features.
$ app/Console/cake bake
List of activities can a bake comand do

$ app/Console/cake bake all


lists possibly available models in your database schema

Press 4 and enter key then y for the required question.


You can now create and edit jobs. Try to leave a required field blank, or try to enter an invalid
date. Thats right, CakePHP has created basic validation rules by introspecting the database
schema.

The Controller and The View


We are going to customize the basic job model we created previously. The job
model already has all the code we need for Jobeet:
A page to list all jobs
A page to create a new job
A page to update an existing job
A page to delete a job
Although the code is ready to be used as is, we will refactor the templates to match closer to
the Jobeet mockups.
The MVC Architecture
If you are used to developing PHP websites without a framework, you probably use the one
PHP file per HTML page paradigm. These PHP files probably contain the same kind of
structure: initialization and global configuration, business logic related to the requested page,
database records fetching, and finally HTML code that builds the page.
You may use a templating engine to separate the logic from the HTML. Perhaps you use a
database abstraction layer to separate model interaction from business logic. But most of the
time, you end up with a lot of code that is a nightmare to maintain. It was fast to build, but
over time, its more and more difficult to make changes, especially because nobody except
you understands how it is built and how it works.
As with every problem, there are nice solutions. For web development, the most common
solution for organizing your code nowadays is the MVC design pattern 24 . In short, the MVC
design pattern defines a way to organize your code according to its nature. This pattern
separates the code into three layers:
The Model layer defines the business logic (the database belongs to this layer).
The View is what the user interacts with (a template engine is part of this layer).
The Controller is a piece of code that calls the Model to get some data that it
passes to the View for rendering to the client.
The Layout
First, if you have a closer look at the mockups, you will notice that much of each page looks
the same. You already know that code duplication is bad, whether we are talking about HTML
or PHP code, so we need to find a way to prevent these common view elements from resulting
in code duplication.
One way to solve the problem is to define a header and a footer and include them in each
template:
But here the header and the footer files do not contain valid HTML. There must be a better
way. Instead of reinventing the wheel, we will use another design pattern to solve this
problem: the decorator design pattern 25 . The decorator design pattern resolves the problem
the other way around: the template is decorated after the content is rendered by a global
template, called a layout.
A layout contains presentation code that wraps around a view. Anything you want to see in all of
your views should be placed in a layout.
CakePHPs default layout is located at /app/View/Layouts/default.ctp. If you want to
change the overall look of your application, then this is the right place to start, because controllerrendered view code is placed inside of the default layout when the page is rendered.
Other layout files should be placed in /app/View/Layouts. When you create a layout, you
need to tell CakePHP where to place the output of your views. To do so, make sure your layout
includes a place for $this->fetch('content');
///index and add is need

The Job Page Template


Now lets customize the template of the job page. Open the view.ctp file and
replace its content with the following code:
<?php echo $this->Html->css('job.css') ?>
<div id="job">
<h1><?php echo h($job['Job']['company']); ?></h1>
<h2><?php echo h($job['Job']['location']); ?></h2>
<h3><?php echo h($job['Job']['position']); ?>
<small>-<?php echo h($job['Job']['type']); ?></small>
</h3>
<?php if($job['Job']['logo']): ?>
<div class="logo">
<a href="<?php echo $job['Job']['url'] ?>">
<img src="http://www.symfony-project.org/uploads/jobs/<?
php echo $job['Job']['logo']?> alt="<?php echo $job['Job']['company'] ?> logo" /></a>
</div>
<?php endif ?>
<div class="description">
<?php echo h($job['Job']['description']); ?>
</div>
<h4>How to apply?</h4>
<p class="how_to_apply">
<?php echo h($job['Job']['how_to_apply']); ?></p>
<div class="meta">
<small>posted on <?php echo h($job['Job']['created']); ?></small>
</div>
<div style="padding: 20px 0">
<?php echo $this->Html->link(__('Edit Job'), array('action' => 'edit',
$job['Job']['id'])); ?>
</div>
</div>

Anda mungkin juga menyukai