Top Banner
CakePHP Rapid Development Framework
20

CakePHP

Oct 31, 2014

Download

Education

CakePHP is a nice way of using MVC architecture in you PHP environment. Looking through this presentation you'll get introduced to MVC and get some start up code examples for you to explore.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CakePHP

CakePHPRapid Development Framework

Page 2: CakePHP

Why CakePHP?MVC

DRY

ORM

Convention over Configuration

Architecture for developing, maintaining, and deploying applications

Page 3: CakePHP

Basic Features• Model, View, Controller Architecture

• Application Scaffolding

• Code generation via Bake

• Helpers for HTML, Forms, Pagination, AJAX, Javascript, XML, RSS and more

• Access Control Lists and Authentication

• Simple yet extensive validation of model data

Page 4: CakePHP

Basic Features cont.• Router for mapping urls and handling

extensions

• Security, Session, and RequestHandler Components

• Utility classes for working with Files, Folders, Arrays and more

Page 5: CakePHP

What is Cake & Why?• Free & Open Source.

• Rapid Development Frame Work Based Around MVC Methodology.

• Templating, Helpers, Components. In short reusable code!

• Built in Email, Cookie, Security, Session & Request Handling Components.

• Caching.

Page 6: CakePHP

Modle View Controller• Model View Controller design patter is based

upon separating your code into three sections.

– The Model represents the application data.

– The View renders a presentation of the model data.

– The Controller handles & routes requests made by the client.

Page 7: CakePHP

Modle View Controller

Page 8: CakePHP

Basic Principles of CakePHP• Conventions

• Extensions

– Controller Extensions - Components

– View Extensions - Helpers

– Model Extensions - Behaviors

– CSS/HTML - Elements

Page 9: CakePHP

How does it work?

www.mycakeapp.com/controller/action/params/

Page 10: CakePHP

Use Bake

The CakePHP Bake console can create any of CakePHP’s basic ingredients: models, views and controllers. And I am not just talking skeleton classes: Bake can create a fully functional application in just a few minutes.

Page 11: CakePHP

Use BakeIn order to use the bakery, locate the cake bash

script in your path and make it executable (make sure you have access rights to the path, using sudo -i or equivalent):

$ cp cake/console/cake* /usr/local/bin/$ mv cake /usr/cake$ chmod 755 /usr/local/bin/cake

Using the Bakery$ cake –help

[..]

acl [CORE] i18n [CORE]

api [CORE] schema [CORE]

bake [CORE] testsuite [CORE]

console [CORE]

Page 12: CakePHP

Let us create the tables for our application which will be a jobs board:

CREATE TABLE IF NOT EXISTS `categories` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM;

CREATE TABLE IF NOT EXISTS `jobs` ( `id` int(11) NOT NULL AUTO_INCREMENT, `category_id` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `body` text COLLATE utf8_unicode_ci, `company` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `job_type` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL, `created` datetime DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM;

CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `pass` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `location` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `address` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `email` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `web` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM;

Page 13: CakePHP

Enter this code in your command prompt$ cd /var/www/newcakeapp

$ cake bake all

[..](Press) 1 (for categories)

Bake generated the links for List jobs and New job, but it is not working at this moment as we did not bake files for jobs table yet.

Repeat cake bake all as necessary.

Now, point your browser to your categories controller. Go ahead and try to add an empty category. Validation is working, but messages need some tweaking. Notice how CakePHP added a Pagination below the table with Categories. Nice. And if you click on column names, data is sorted by that column!

Page 14: CakePHP

Typical Request

Page 15: CakePHP

Controllers pass args to model

$this->set(‘view_var’, $controller_var);

Page 16: CakePHP

Built-in HelpersReusable code (DRY) accessible to any view.

Paginator

RSS

Session

Text

Time

• AJAX

• Cache

• Form*

• HTML*

• Javascript*

• Number

• Paginator

Page 17: CakePHP

Built-in ComponentsReusable code (DRY) accessible to any controller.

• Acl

• Auth

• Session

• RequestHandler

• Security

• Email

• Cookie

Page 18: CakePHP

Directories• Config - Holds the application configuration

files.

– Database connection

– Bootstrap

– Core

• Controllers - Contains your application controller & components.

• Models - Contains your application models, behaviors & data sources.

Page 19: CakePHP

Directories• Tmp - This is where CakePHP stores temporary

data.

• Vendors - Third Party Libraries.

• Views - What the user sees.

• Webroot - Contains CSS/JS/Flash/Etc Files

Page 20: CakePHP

Resources• http://book.cakephp.org

• http://bakery.cakephp.org

• #cakephp on irc.freenode.net

• http://cakephp.org/screencasts

• http://logs.cakephp.nu

• http://groups.google.com/group/cake-php

• http://trac.cakephp.org