streamlining your applications with web frameworks
DESCRIPTION
Presentation by Nate Abele from CakePHP about web development frameworks.TRANSCRIPT
Streamlining Your Applications with Web
FrameworksKings of Code ~ May 27th, 2008
What is a Web Framework?
• “...software framework that is designed to support the development of dynamic websites, Web applications and Web services.” - Wikipedia
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
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
Continuum of Structure
CakePHP CodeIgniter Zend Framework
More Less
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
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
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
Why Cake?• Other PHP frameworks
• Zend Framework
• Symfony
• PHP on Trax
• CodeIgniter
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
An MVC Quickie
• Primary: separation between Controller and View, to partition business logic and presentation
• Secondary: separation between data (Model) and Controller
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
<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>
An MVC Quickie
The Fun Stuff
Ajax!• Normal Link:
$html->link(‘Add Post’, ‘/posts/add’);
• Ajax Link:$ajax->link(‘Add Post’, ‘/posts/add’, array(‘update’ => ‘addPostDiv’,‘complete’ => ‘Effect.SlideDown(“addPostDiv”)’
));
REST & Resources!
• // config/routes.php
Router::mapResources(“posts”);
Router::parseExtensions(“rss”, “js”);
• // app_controller.php
var $components = array(“RequestHandler”);
REST & Resources!// views/posts/xml/index.ctp
<posts>
<?php echo $xml->serialize($posts); ?>
</posts>
// views/posts/xml/view.ctp
<?php echo $xml->serialize($posts); ?>
REST & Resources!
// views/widgets/js/index.ctp
<?php echo $javascript->object($posts); ?>
// views/widgets/js/view.ctp
<?php echo $javascript->object($posts); ?>
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)
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” />
The Future...