You are on page 1of 6

INTRODUCTION OF CAKEPHP

Mohamad Zaki mohdzaki04@gmail.com

Introduction

CakePHP => Open Source + FREE


Website => http://cakephp.org
Some Features:
Active Framework Since 1998 (Cakephp-1.2)
Friendly Community
Capability with latest version 4 and 5 of PHP
MVC Architecture
Build-in Validation

MVC

CakePHP follows the MVC software design pattern.


The Model - represents the application data or another word, model related directly with
tables in database.
The View - renders a presentation of model data
The Controller - handles and routes request made by the user using GET and POS method.

CakePHP Folder Structure

CakePHP folder:
App
Cake {The core/libraries file of cake is here}
Vendor {3rd party PHP libraries is here}
.htaccess
index.php
README

App Folder (important)


Config
All configuration file CakePHP uses. Such as Database, Bootstrapping,
Core, Email and more should can be out here.
Controller

Contains your applications controller and their components.

Model

Contains your applications model, behaviors and data sources.

Plugin

Contain for sub application to support main application. This folder same
like other app folder cakephp.

View

Presentation(display) file and other file replace here: elements, errors pages,
helpers, layouts, email template and view files

Webroot

This folder as documents root folder for your application. Folder here also
server as holding places for CSS, Images, Script file, Uploaded Files.

INTRODUCTION OF CAKEPHP
Mohamad Zaki mohdzaki04@gmail.com

Cakephp Conventions
Cakephp Conventions takes a bit of time to learn and implementation. Convention also makes for a
very uniform system development, allowing other developers to jump in and help more easily.
CakePHP have its own naming convention standards to be used during PHP application
development. Whenever you have to create application using CakePHP you will need to create
following files first.

One File to define your model.

One File to define your controller.

One or More files to represent the OUTPUT called view files.

So, while creating above files you will have to follow CakePHP naming convention standards. And
its very important to the PHP developer to follow these standards.
A) Database table and field naming convention

Table name must be in lowercase & plural. If there are more than one word in the table
name then words should be separated by underscore.
Example : users, consultant_services, etc.

Field-Name should also be in lowercase and if more than one word then separated by
underscore.
Example : id, consultant_service_name

ForeignKey name should be singular and tablename_id


Example : users_id or consultant_services_id
EXAMPLE:
table posts belongto table users
CREATE TABLE post_contents ( //more than one word separated by underscore
id PRIMARY KEY,
user_id INT, //ForeignKey name
title VARCHAR(50),
body TEXT,
);

table users hasMany table posts


CREATE TABLE users ( //name one word must be in lowercase & plural
id PRIMARY KEY,
full_name VARCHAR(50), //more than one word, separated by underscore
username VARCHAR(50),
password VARCHAR(50),
email VARCHAR(100)
);

INTRODUCTION OF CAKEPHP
Mohamad Zaki mohdzaki04@gmail.com

B) Model naming Convention

Model file names are upper camel case, more than one word should be separated by
underscore.
Example : User.php or Consultant_service.php
Model class name are singular, usually this should be the database table name but in singular
format.
Example : User or ConsultantService
EXAMPLE 1:
Path : /app/Model/User.php
Coding Content:
class User extends AppModel {
public $name = 'User';
}

EXAMPLE 2:
Path : /app/Model/User_profile.php
Coding Content:
class UserProfile extends AppModel {
public $name = 'UserProfile';
}

C) Controller naming Convention

Controller file names are plural and upper camel case, and filenames also end with
Controller.php.
Example : UsersController.php or ConsultantservicesController.php
EXAMPLE 1:
Path : /app/Controller/UsersController.php
Coding Content:
class UsersController extends AppController {
}

EXAMPLE 2:
Path : /app/Controller/UserProfilesController.php
Coding Content:
class UserProfilesController extends AppController {
}

INTRODUCTION OF CAKEPHP
Mohamad Zaki mohdzaki04@gmail.com

Multiple word controllers can be any inflected form which equals the controller name so:

/userProfiles url : http://www.example.com/userProfiles


/UserProfiles url : http://www.example.com/UserProfiles
/User_profiles url : http://www.example.com/User_profiles
/red_apples url : http://www.example.com/user_profiles

D) View naming Convention

View files are located at /app/views and into that a folder is created with its respective
controller name, if more than one word then separated by underscore.
View file names are function_name.ctp (The function name is the name of the function you
have defined in your appropriate Controller file.)
EXAMPLE 1:
Path : /app/View/Users
Function name in UsersController.php
class UsersController extends AppController {
function index(){
....
}
}

EXAMPLE 2:
Path : /app/View/UserProfiles
Function name in UsersController.php
class UserProfilesController extends AppController {
function index(){
....
}
}

File Names : index.ctp


Coding Content :
<h1>Hello World</h1>
<p>Welcome to cakephp</p>
<?php echo Read the Book Anywhere ?>

Other Convention in Cakephp

Components
Path : /app/Controller/Component
Name camelcased: GoogleTranslateComponent filename: google_translate.php
Helpers
Path : /app/View/Helper
Name camelcased: GoogleHelper filename: google.php

INTRODUCTION OF CAKEPHP
Mohamad Zaki mohdzaki04@gmail.com

Cakephp Configuration

Cakephp Configuration needed


Database Configuration
CakePHP expects database configuration details to be in a file at
app/Config/database.php. An example database configuration file can be
found at app/Config/database.php.default. A finished configuration
should look something like this:
class DATABASE_CONFIG
public $default =
'datasource'
'persistent'
'host'
'login'
'password'
'database'
'prefix'
);
}

{
array(
=> 'Database/Mysql',
=> false,
=> 'localhost',
=> 'cakephpuser',
=> 'c4k3roxx!',
=> 'my_cakephp_project',
=> ''

Core Configuration (important only)


Each application in CakePHP contains a configuration file,
app/Config/core.php, to determine CakePHPs internal behavior.
Below is a description of each variable and how it affects your CakePHP application.
Debug Changes CakePHP debugging output.
0 = Production mode. No output.
1 = Show errors and warnings.
2 = Show errors, warnings, and SQL.
[SQL log is only shown when you add $this->element(sql_dump) to your
view or layout.]

Configure
Use write() to store data in the applications configuration:
Configure::write('Faculty.name','Computer Sains');
Configure::write('Company.code','CSXXXX');

Use read() to read configuration data from the application


Configure::read('Faculty.name'); //Output : Computer Sains
Configure::read('Faculty.code '); //Output : CSXXXX

Routes Configuration
Routes in an application are configured in app/Config/routes.php
Default Routing
URL pattern default routes:
http://example.com/controller/action/param1/param2/param3

The URL /posts/view maps to the view() action of the PostsController, and
/products/view_clearance maps to the view_clearance() action of the
ProductsController. If no action is specified in the URL, the index() method is
assumed.

INTRODUCTION OF CAKEPHP
Mohamad Zaki mohdzaki04@gmail.com

The default routing setup also allows you to pass parameters to your actions using
the URL. A request for /posts/view/25 would be equivalent to calling view(25) on
the PostsController.
Default Example:
Controller

Action

Param

URL

UsersController

index()

null

/users/index
/users/

UsersController

view(param)

132

/users/view/123

Custome Example 1:
Router::connect(
'/senarai_pengguna/',
array('controller' => 'users', 'action' => 'index')
);

Custome Example 2:
Router::connect(
'/maklumat_pengguna/*',
array('controller' => 'users', 'action' => 'index')
);

Controller

Action

Param URL

UsersController

index()

null

/users/senarai_pengguna/
/users/senarai_pengguna/index

UsersController

view(param)

132

/users/maklumat_pengguna/123

Next Continue for more Detail


Controller
Find All, Find List, Find First, Find By, Find with Conditions, Find indecude ascending,
groupBy, Field.

Model
HasMany, BelongTo, Validation
Helpers
Form Helpers
HTML Helpers
CSS, Script Helpers
Number Helper

You might also like