Caketwit - Twitter Like Service using CakePHP

29
CakeTwit Johnathan Pulos @ Missional Digerati Image by Blog.SpoonGraphics.co.uk

description

ICCM Europe 2013 Presentation: Do you want to build PHP websites faster? I think we all do. In this workshop, we will take a brief look at the CakePHP open-source PHP framework that makes this possible. As we build a simple Twitter like service, called CakeTwit, on the CakePHP Framework, you will learn the benefits of using a framework in your development. CakePHP provides code generation, Model View Controller Architecture, helper libraries for faster development, and much more! If you want make programming a piece of cake, check out this workshop.

Transcript of Caketwit - Twitter Like Service using CakePHP

Page 1: Caketwit - Twitter Like Service using CakePHP

CakeTwitJohnathan Pulos @ Missional Digerati

Image by Blog.SpoonGraphics.co.uk

Page 2: Caketwit - Twitter Like Service using CakePHP

About MeBackground

PHP - 9 Yrs.

Ruby - 4 Yrs.

iOS - 2 Yrs.

Twitter: @jpulosGithub: codemisEmail: [email protected]

Page 3: Caketwit - Twitter Like Service using CakePHP

Missional Digerati

Community of thinkers, designers and developers committed to leveraging technology to advance the pace of reaching every person on earth with the good news

of Jesus Christ.

Twitter: @M_Digerati Github: MissionalDigerati

Website: MissionalDigerati.org

Page 4: Caketwit - Twitter Like Service using CakePHP

Presentation ResourcesCode on Github: http://goo.gl/f6eWb

Page 5: Caketwit - Twitter Like Service using CakePHP

Basic Ingredients

Page 6: Caketwit - Twitter Like Service using CakePHP

Model View Controller

Model - classes that handle business logic of data.

Controller - traffic cop between models and view.

View - renders a presentation of modeled data.

http://goo.gl/yRDaa

Page 7: Caketwit - Twitter Like Service using CakePHP

Convention Over Configuration

http://goo.gl/N2Nh0

DB TABLES

ex. cars, user_scores

plural + underscored

MODELS

ex. Car, UserScore

singular + CamelCased

CONTROLLERS

ex. CarsController, UserScoresController

plural + CamelCased + ending in “Controller”

VIEWS

ex. add, view_user

action name + underscored

Page 8: Caketwit - Twitter Like Service using CakePHP

Create Read Update DeleteImage by IconEden.com

Page 9: Caketwit - Twitter Like Service using CakePHP

<?phpApp::uses('AppController', 'Controller');

class TweetsController extends AppController {

public function index() { /** * Show All Tweets */ }

public function view($id = null) { /** * Show an Existing Tweet */ }

public function add() { /** * Add a New Tweet */ }

public function edit($id = null) { /** * Edit an Existing Tweet */ }

public function delete($id = null) { /** * Delete an Existing Tweet */ }}

Page 10: Caketwit - Twitter Like Service using CakePHP

CakeTwit

XPMac OSX LionMAMP 2.1.1 (mamp.info)

PHP 5.4.4Apache 2.2.22MySQL 5.5.25

Sequel Pro (sequelpro.com)Textmate (macromates.com)Terminal

Windows XPWAMP 2.2E (wampserver.com)

PHP 5.4.3Apache 2.2.2MySQL 5.5.24

PHPMyAdmin 3.5.1Sublime Text (sublimetext.com)Command Line

Development Environments

Image by Blog.SpoonGraphics.co.uk & Icons by PinkMoustache.net

Page 11: Caketwit - Twitter Like Service using CakePHP

CakeTwitOff Screen Preparation

http://goo.gl/lKoOtXP

http://goo.gl/om8K6

Get CakePHP 2.3 Beta - https://github.com/cakephp/cakephp/tags

Create a Virtual Host

Enable Mod Rewrite

Create a Database

Add PHP to System Path

Image by Blog.SpoonGraphics.co.uk & Icons by PinkMoustache.net

Page 12: Caketwit - Twitter Like Service using CakePHP

Go Green!Image by DryIcons.com

Page 13: Caketwit - Twitter Like Service using CakePHP

NOTICENotice (1024): Please change the value of 'Security.salt' in

app/Config/core.php to a salt value specific to your application [CORE/Cake/Utility/Debugger.php, line 835]

CakeTwitGo Green!

Image by Blog.SpoonGraphics.co.uk

<?php/** * File: app/Config/core.php * Line: 187 *//** * A random string used in security hashing methods. */ Configure::write('Security.salt', 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi');

Change to Random Alphanumeric String

Page 14: Caketwit - Twitter Like Service using CakePHP

NOTICENotice (1024): Please change the value of 'Security.cipherSeed' in

app/Config/core.php to a numeric (digits only) seed value specific to your application [CORE/Cake/Utility/Debugger.php, line 839]

CakeTwitGo Green!

Image by Blog.SpoonGraphics.co.uk

<?php/** * File: app/Config/core.php * Line: 192 *//** * A random numeric string (digits only) used to encrypt/decrypt strings. */ Configure::write('Security.cipherSeed', '76859309657453542496749683645');

Change to Random Numeric String (Digits Only)

Page 15: Caketwit - Twitter Like Service using CakePHP

NOTICE

Your database configuration file is NOT present.

CakeTwitGo Green!

Image by Blog.SpoonGraphics.co.uk & Icons by PinkMoustache.net

app/Config/database.php.default app/Config/database.php

Page 16: Caketwit - Twitter Like Service using CakePHP

NOTICE

Your database configuration file is NOT present.

CakeTwitGo Green!

Image by Blog.SpoonGraphics.co.uk

<?php/** * File: app/Config/database.php */class DATABASE_CONFIG {

public $default = array( 'datasource' => 'Database/Mysql', 'persistent' => false, 'host' => 'localhost', 'login' => 'user', 'password' => 'password', 'database' => 'database_name', 'prefix' => '', //'encoding' => 'utf8', );}

Set Your MySql Settings

Page 17: Caketwit - Twitter Like Service using CakePHP

Call Me Betty CrockerImage by DryIcons.com

Page 18: Caketwit - Twitter Like Service using CakePHP

CakeTwitNaming Convention for Tweets

Image by Blog.SpoonGraphics.co.uk & Icons by PinkMoustache.net

DB TABLE

tweets

MODEL

Tweet

CONTROLLER

TweetController

Page 19: Caketwit - Twitter Like Service using CakePHP

CakeTwitTweets Database Table

Image by Blog.SpoonGraphics.co.uk

Import SQL into Database - http://goo.gl/4MHJj

CREATE TABLE `tweets` (

`id` int(11) unsigned NOT NULL AUTO_INCREMENT,

`post` varchar(255) DEFAULT NULL,

`author` varchar(255) DEFAULT NULL,

`created` datetime DEFAULT NULL,

`modified` datetime DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `tweets` (`id`, `post`, `author`, `created`, `modified`)

VALUES

(1,'I LUV CakeTwit!!!','A Random Poet','2012-12-24 08:00:00','2012-12-24 08:00:00'),

(2,'Give me another piece of Cake, Twit that is!','Poetic Justice','2012-12-23 08:00:00','2012-12-23 08:00:00');

Page 20: Caketwit - Twitter Like Service using CakePHP

CakeTwit

XP

Bake the Tweet’s Model

Image by Blog.SpoonGraphics.co.uk & Icons by PinkMoustache.net

In Terminal`cd` into the working directory

Select “M” for modelFollow the interactive console

For displayField select postAdd a validation to post as “notempty”

on Command Line`cd` into the working directory

Select “M” for modelFollow the interactive console

For displayField select postAdd a validation to post as “notempty”

> app/Console/cake bake > app\Console\cake bake

Page 21: Caketwit - Twitter Like Service using CakePHP

CakeTwit

XP

Bake the Tweet’s Controller

Image by Blog.SpoonGraphics.co.uk & Icons by PinkMoustache.net

In Terminal`cd` into the working directory

Follow the interactive consoleBuild the controller interactively

No dynamic scaffolding

Use basic class methods

on Command Line`cd` into the working directory

Follow the interactive consoleBuild the controller interactively

No dynamic scaffolding

Use basic class methods

> app/Console/cake bake Controller > app\Console\cake bake Controller

Page 22: Caketwit - Twitter Like Service using CakePHP

CakeTwit

XP

Bake the Tweet’s Views

Image by Blog.SpoonGraphics.co.uk & Icons by PinkMoustache.net

In Terminal`cd` into the working directory

Follow the interactive console

on Command Line`cd` into the working directory

Follow the interactive console

> app/Console/cake bake View > app\Console\cake bake View

Page 23: Caketwit - Twitter Like Service using CakePHP

The Real Van GoghImage by DryIcons.com

Visit your local website, and see your master piece!

Page 24: Caketwit - Twitter Like Service using CakePHP

Other Benefits

Page 25: Caketwit - Twitter Like Service using CakePHP

http://goo.gl/dXzjq

Model DataSource Data

DataSourcesThe link between models and the source of data that models represent.

MySql Sqlite Postgres Sqlserver

Icons by PinkMoustache.net & DefaultIcon.com

Page 26: Caketwit - Twitter Like Service using CakePHP

http://goo.gl/17M6G + http://goo.gl/44OhY

BehaviorsReusable logic for the models. A mixin with callbacks.

Access Control List

Containable

Translate

Tree

Icons by PinkMoustache.net

Models Behavior

Page 27: Caketwit - Twitter Like Service using CakePHP

http://goo.gl/Mp2NO + http://goo.gl/i53PM

ComponentsReusable logic for controllers.

SecuritySessionsAccess Control ListsEmails

CookiesAuthenticationRequest HandlingPagination

Icons by PinkMoustache.net

Controllers Components

Page 28: Caketwit - Twitter Like Service using CakePHP

http://goo.gl/8Gl8c + http://goo.gl/F2Ju5

HelpersPresentational logic shared among views, elements, and layouts.CacheFormHTMLJavascript (JS)

Pagination (Paginator)

RSSSession

TextTime

Icons by PinkMoustache.net

Views

Helpers

Elements

Layouts