22
Streamlining Your Applications with Web Frameworks Kings of Code ~ May 27th, 2008

Streamlining Your Applications with Web Frameworks

Embed Size (px)

DESCRIPTION

Presentation by Nate Abele from CakePHP about web development frameworks.

Citation preview

Page 1: Streamlining Your Applications with Web Frameworks

Streamlining Your Applications with Web

FrameworksKings of Code ~ May 27th, 2008

Page 2: Streamlining Your Applications with Web Frameworks

What is a Web Framework?

• “...software framework that is designed to support the development of dynamic websites, Web applications and Web services.” - Wikipedia

Page 3: Streamlining Your Applications with Web Frameworks

What is a Web Framework?

• A set of tools and a way to organize them

• NOT an application

• An abstract base from which to build

• NOT a CMS

Page 4: Streamlining Your Applications with Web Frameworks

What is a Web Framework?

• Provide libraries for common tasks

• Provide structure and convention for your application code

• Promote best practices in application design/architecture

Page 5: Streamlining Your Applications with Web Frameworks

Continuum of Structure

CakePHP CodeIgniter Zend Framework

More Less

Page 6: Streamlining Your Applications with Web Frameworks

Why Use a Framework?

• Lets you focus on what’s important

• The hard problems have already been solved by people smarter than you

• Benefits of community

• You (and your requirements) are not special

• Chances are, you already do

Page 7: Streamlining Your Applications with Web Frameworks

Why Cake?• For a lot of people and a lot of reasons,

Rails = FAIL

• Difficult and expensive to deploy

• Ahem! scaling...

• Programmer availability

• PHP is still by far the #1 web language

Page 8: Streamlining Your Applications with Web Frameworks

Why Cake?• “Oh Rasmus, why do you engage in this

‘virtual crap-flinging’? Can’t you lead by example like David Heinemeier Hansson? That guy is the height of maturity and an expert scalability guy.”

“...look at the top 100 websites on the internet: about 40% of them are written in PHP and 0% of them are written in Rails.” - Terry Chay

Page 9: Streamlining Your Applications with Web Frameworks

Why Cake?• Other PHP frameworks

• Zend Framework

• Symfony

• PHP on Trax

• CodeIgniter

Page 10: Streamlining Your Applications with Web Frameworks

An MVC QuickieDispatcher

Controller

View

Model

The Dispatcher requests the appropriate Controller/action,

which interacts with the Model

The Controller then sends the results of its operations to the

view, where it is rendered

Page 11: Streamlining Your Applications with Web Frameworks

An MVC Quickie

• Primary: separation between Controller and View, to partition business logic and presentation

• Secondary: separation between data (Model) and Controller

Page 12: Streamlining Your Applications with Web Frameworks

An MVC Quickie/* models/post.php */

class Post extends AppModel { }

/* controllers/posts_controller.php */class PostsController extends AppController {

function index() {// Get the data from the Model$posts = $this->paginate();

// Send the data to the view$this->set(compact(‘posts’));

}}

A simple example

Page 13: Streamlining Your Applications with Web Frameworks

<div class="posts index"><h2>Posts</h2><p><?=$paginator->counter(); ?></p>

<table cellpadding="0" cellspacing="0"><tr> <th><?=$paginator->sort('id'); ?></th> <th><?=$paginator->sort('title'); ?></th> <th><?=$paginator->sort('body'); ?></th> ... <th class="actions">Actions</th></tr><? foreach ($posts as $post) { ?> <tr> <td><?=$post->id; ?></td> <td><?=$post->title; ?></td> <td><?=$post->body; ?></td> .... <td class="actions">...</td> </tr><? } ?></table>

<div class="paging"><?=$paginator->prev('<< previous', array(), null, array('class' => 'disabled')); ?> |<?=$paginator->numbers(); ?><?=$paginator->next('next >>', array(), null, array('class' => 'disabled')); ?></div>

Page 14: Streamlining Your Applications with Web Frameworks

An MVC Quickie

Page 15: Streamlining Your Applications with Web Frameworks

The Fun Stuff

Page 16: Streamlining Your Applications with Web Frameworks

Ajax!• Normal Link:

$html->link(‘Add Post’, ‘/posts/add’);

• Ajax Link:$ajax->link(‘Add Post’, ‘/posts/add’, array(‘update’ => ‘addPostDiv’,‘complete’ => ‘Effect.SlideDown(“addPostDiv”)’

));

Page 17: Streamlining Your Applications with Web Frameworks

REST & Resources!

• // config/routes.php

Router::mapResources(“posts”);

Router::parseExtensions(“rss”, “js”);

• // app_controller.php

var $components = array(“RequestHandler”);

Page 18: Streamlining Your Applications with Web Frameworks

REST & Resources!// views/posts/xml/index.ctp

<posts>

<?php echo $xml->serialize($posts); ?>

</posts>

// views/posts/xml/view.ctp

<?php echo $xml->serialize($posts); ?>

Page 19: Streamlining Your Applications with Web Frameworks

REST & Resources!

// views/widgets/js/index.ctp

<?php echo $javascript->object($posts); ?>

// views/widgets/js/view.ctp

<?php echo $javascript->object($posts); ?>

Page 20: Streamlining Your Applications with Web Frameworks

REST & Resources!GET /posts.xml HTTP/1.1

GET /posts/1.xml HTTP/1.1

POST /posts.xml HTTP/1.1

PUT /posts/1.xml HTTP/1.1

DELETE /posts/1.xml HTTP/1.1

PostsController::index()

PostsController::view(1)

PostsController::add()

PostsController::edit(1)

PostsController::delete(1)

Page 21: Streamlining Your Applications with Web Frameworks

REST & Resources!

POST /posts.xml HTTP/1.1Host: www.example.orgContent-Type: text/xml; charset=utf-8

Content-Length: 67

<?xml version=“1.0”?><post title=“XML” body=“New Post from XML” />

Page 22: Streamlining Your Applications with Web Frameworks

The Future...