cakephp

Post on 31-Oct-2014

5 Views

Category:

Education

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

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.

TRANSCRIPT

CakePHPRapid Development Framework

Why CakePHP?MVC

DRY

ORM

Convention over Configuration

Architecture for developing, maintaining, and deploying applications

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

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

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.

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.

Modle View Controller

Basic Principles of CakePHP• Conventions

• Extensions

– Controller Extensions - Components

– View Extensions - Helpers

– Model Extensions - Behaviors

– CSS/HTML - Elements

How does it work?

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

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.

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]

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;

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!

Typical Request

Controllers pass args to model

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

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

Paginator

RSS

Session

Text

Time

• AJAX

• Cache

• Form*

• HTML*

• Javascript*

• Number

• Paginator

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

• Acl

• Auth

• Session

• RequestHandler

• Security

• Email

• Cookie

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.

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

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

top related