• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
IPC 2008: Agavi (2008-10-28)
 

IPC 2008: Agavi (2008-10-28)

on

  • 1,627 views

My presentation "Scaling Your Development With The Agavi Framework" I gave at International PHP Conference 2008 in Mainz, Germany

My presentation "Scaling Your Development With The Agavi Framework" I gave at International PHP Conference 2008 in Mainz, Germany

Statistics

Views

Total Views
1,627
Views on SlideShare
1,626
Embed Views
1

Actions

Likes
0
Downloads
13
Comments
0

1 Embed 1

http://192.168.10.100 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    IPC 2008: Agavi (2008-10-28) IPC 2008: Agavi (2008-10-28) Presentation Transcript

    • Scaling Your Development with the Agavi Framework
    • David Zülke
    • David Zuelke
    • (assuming everyone kmows where Germany is, I’ll move on)
    • • Bitextender GmbH, based in Munich.de • We’ve been doing development, consulting and training for 5+ years. • Team of 7. • The company behind Agavi, backing and developing it.
    • But let’s move on to the non-boring stuff, shall we?
    • From Mojavi to Agavi A brief history lesson.
    • Anyone heard of Mojavi before, or used it?
    • Mojavi 1, 2 and 3 were developed by Sean Kerr. Development wasn’t open and transparent, despite the license (and awesomeness!), and died eventually. Mojavi 3 was forked by some guys from Keller, Texas, and given the name Agavi in 2005. Because Agave plants grow in the Mojave desert. Since late 2005, Agavi is maintained by Bitextender. symfony is also based on Mojavi 3.
    • Speaking of Mojavi... Sean Kerr is back from the dead, and loves Agavi!
    • Agavi Key Features and Differences.
    • No Assumptions Being PHP based, it works best for websites and other HTTP-based stuff, but you can use it to write any app. No requirements for specific template engines, DBMSes, ORMs, client side JS libraries etc. Abstraction of HTTP request method verbs, output types, response implementations etc. Form Handling is independent of libraries, template engines etc.
    • Reuse Code The right things are done in the right places, and the framework prevents common mistakes. Exposing Actions of a web application through a SOAP web service API etc. can be done in minutes. Want an RSS feed of your latest products? It’s just a new output type away. Want to return JSON for Ajax features? Have it done when a JS framework sends the right request headers!
    • Environments and Contexts An Environment is bootstrapped for every box or developer. Could be “production”, or “dev-joecool” etc. A Context represents a way of accessing the application, like “web”, “soap” or “console”. Any configuration can be specific to one or more Environment(s) and/or Context(s). → no more copying and overwriting of DB configs!
    • Modules, Actions, Views etc An Application has a number of Modules. Each Module has Actions with corresponding Views and Templates, as well as various configuration files that control caching, validation and so on. Actions, which can also be nested into folders, contain application logic, make calls to Models, and have one or more Views (“Error”, “Success”, “Input” etc.) Views handle presentation, usually using Templates.
    • Execution Containers Every Action execution happens in an isolated Container. Every container has it’s own request data, response, filters etc. A normal execution is idempotent, i.e. does not affect the “outside world”. Internal forwarding can be done by returning a new Container from a View.
    • Filters Global Filters or per-Action/Container filters. Wrap the execution, and call the next Filter in the chain - like an Onion or a Russian Nested Doll. Features like Security or Form Handling are implemented using Filters inside Agavi. Flow can be redirected internally, response info can be modified etc; numerous possibilities.
    • Layers and Layouts A View can leave instructions on Templates to render; these are called Layers. Each Layer has access to the output of the previous Layers, and can define Slots - Containers with Actions that are run before rendering, returning the content. Layer and Slot definitions can be made in a configuration file; the result is a Layout. All this can be done programmatically, as well.
    • Routing Used for matching URLs, SOAP method names. Every route can have children. Callbacks can control and modify behavior on matching or when generating URLs. Routes can also set Output Types, force continuing of execution even though they matched, and much more. Also very nice for refactoring existing applications.
    • Form Handling Form Population Filter makes form handling a breeze. Can pre-populate forms using given values. Re-populates a form when an error occurred. Highlights erroneous fields and labels. Can insert error messages into the document. All without a single line of code in templates.
    • Caching Cache the entire execution of a request, or just parts. Action-based, so a Slot that runs can be cached, too. Output can be cached on a per-layer basis, so that the outer master template always runs even when cached. Also caches cookies (with correct lifetime!), HTTP headers that were set etc. Stampede Protection will be included in Agavi 1.0
    • Validation Validates not only request parameters, but also Files, Cookies and HTTP Headers. Default settings mean you only have access to data you validated. Drastically reduces the possibility for dev mistakes. Validators can have dependencies (“only validate email if checkbox is on”), different severities, handle arrays, normalize values (e.g. make Unix TS from date value).
    • Security Security Filter is the internal mechanism to auto-check against credentials, authentication status etc. Ships with plain and RBAC implementations, easily extensible. Provided implementations work with the standard getCredentials() and isSecure() methods on Actions; any other behavior, additional authentication levels etc can be implemented in a custom Security Filter.
    • Internationalization Bundles the Unicode CLDR database with information about all locales of this world. Functions for translating text, formatting and parsing dates, numbers and currency values, calendar and timezone operations, locale information (e.g. list of all countries in Japanese language etc.) Parts of the functionality are ports of ICU, IBM’s Java/C library for globalization.
    • And no, it is not as slow as Rasmus claimed at FrOSCon.
    • “ For the FrOSCon folks following me - the Agavi numbers were low because I was in development mode - slides updated 126.91 trans/s (not 8.24 trans/s!)
    • Examples A.k.a. real code, the stuff everyone’s here for.
    • An Action that lists products class
Products_LatestAction
extends
AcmeStoreBaseProductsAction
{ 

public
function
execute(AgaviRequestDataHolder
$rd)
{ 



//
grab
the
product
finder
model.
could
also
be
doctrine
etc. 



$pfm
=
$this‐>getContext()‐>getModel('ProductFinder'); 



//
fetch
a
list
of
latest
products,
let's
assume
it's
an
array 



$latestProducts
=
$pfm‐>fetchLatest(): 



//
assign
it
for
the
View 



$this‐>setAttribute('products',
$latestProducts); 



//
go
to
Products_LatestSuccessView 



return
'Success'; 

} }
    • The corresponding View class
Products_LatestSuccessView
extends
AcmeStoreBaseProductsView
{ 

public
function
executeHtml(AgaviRequestDataHolder
$rd)
{ 



//
not
much
to
do
here.
let's
just
load
the
default
layout 



$this‐>setupHtml($rd); 



//
and
maybe
set
a
page
title 



$this‐>setAttribute('title',
'Latest
ACME
Products'); 

} }
    • The template, obviously... <h2><?=$t['title']?></h2> <dl> <?php
foreach($t['products']
as
$p):
?> 

<dt> 



<a
href=quot;<?=$ro‐>gen('products.product.view',
array('id'
=>
$p['id']))?>quot;> 





<?=htmlspecialchars($p['name'])?> 



</a> 

</dt> 

<dd><?=$tm‐>_c($p['price'])?></dd> <?php
endforeach;
?> </dl>
    • And routes to make it work <route
name=quot;productsquot;
pattern=quot;^/productsquot;
module=quot;Productsquot;> 

<route
name=quot;.indexquot;
pattern=quot;^$quot;
action=quot;Indexquot;
/> 

<route
name=quot;.latestquot;
pattern=quot;^/latest$quot;
action=quot;Latestquot;
/> 

<route
name=quot;.productquot;
pattern=quot;^/(id:d+)quot;
action=quot;Productquot;> 



<route
name=quot;.viewquot;
pattern=quot;^$quot;
action=quot;.Viewquot;
/> 



<route
name=quot;.galleryquot;
pattern=quot;^/gallery$quot;
action=quot;.Galleryquot;
/> 

</route> </route>
    • Now let’s add JSON! class
Shop_LatestProductsSuccessView
extends
AcmeStoreBaseShopView
{ 

public
function
executeHtml(AgaviRequestDataHolder
$rd)
{
/*...*/
} 

 

public
function
executeJson(AgaviRequestDataHolder
$rd)
{ 



return
json_encode($this‐>getAttribute('products')); 

} } <route
pattern=quot;application/jsonquot;
source=quot;_SERVER[HTTP_ACCEPT]quot;
stop=quot;falsequot;
output_type=quot;jsonquot;
/> <route
name=quot;productsquot;
pattern=quot;^/productsquot;
module=quot;Productsquot;><!‐‐
...
‐‐></route> <output_type
name=quot;htmlquot;><!‐‐
...
‐‐></output_type> <output_type
name=quot;jsonquot;> 

<parameter
name=quot;http_headersquot;> 



<parameter
name=quot;Content‐Typequot;>application/json</parameter> 

</parameter> </output_type>
    • Or how about CSV? class
Shop_LatestProductsSuccessView
extends
AcmeStoreBaseShopView
{ 

public
function
executeHtml(AgaviRequestDataHolder
$rd)
{
/*...*/
} 

public
function
executeJson(AgaviRequestDataHolder
$rd)
{
/*...*/
} 

public
function
executeCsv(AgaviRequestDataHolder
$rd)
{ 



$csv
=
fopen('php://temp',
'r+'); 



fputcsv($csv,
$this‐>getAttribute('products')); 



rewind($fp); 



return
$fp; 

} } <route
pattern=quot;application/jsonquot;
source=quot;_SERVER[HTTP_ACCEPT]quot;
stop=quot;falsequot;
output_type=quot;jsonquot;
/> <route
pattern=quot;/csv$quot;
stop=quot;falsequot;
cut=quot;truequot;
output_type=quot;csvquot;
/> <route
name=quot;productsquot;
pattern=quot;^/productsquot;
module=quot;Productsquot;><!‐‐
...
‐‐></route> <output_type
name=quot;csvquot;> 

<parameter
name=quot;http_headersquot;> 



<parameter
name=quot;Content‐Typequot;>text/csv</parameter> 

</parameter> </output_type>
    • Form Handling
    • Didn’t login as Chuck Norris!
    • This is the Template file: <form
action=quot;<?php
echo
$ro‐>gen('login');
?>quot;
method=quot;postquot;> 

<dl> 



<dt><label
for=quot;fe‐usernamequot;>Username:</label></dt> 



<dd><input
type=quot;textquot;
name=quot;usernamequot;
id=quot;fe‐usernamequot;
/></dd> 



<dt><label
for=quot;fe‐passwordquot;>Password:</label></dt> 



<dd><input
type=quot;passwordquot;
name=quot;passwordquot;
id=quot;fe‐passwordquot;
/></dd> 



<dt>&#160;</dt> 



<dd><input
type=quot;checkboxquot;
name=quot;rememberquot;
id=quot;fe‐rememberquot;
/><label
for=quot;fe‐rememberquot;>
Log
 me
in
automatically.</label></dd> 



<dt>&#160;</dt> 



<dd><input
type=quot;submitquot;
value=quot;Loginquot;
/></dd> 

</dl> </form>
    • A Layout: 

<configuration
context=quot;webquot;> 



<output_types
default=quot;htmlquot;> 





<output_type
name=quot;htmlquot;> 







<renderers
default=quot;phpquot;> 









<renderer
name=quot;phpquot;
class=quot;AgaviPhpRendererquot;> 











<!‐‐
renderer
options
you
want
‐‐> 









</renderer> 







</renderers> 







<layouts
default=quot;defaultquot;> 









<layout
name=quot;defaultquot;> 











<layer
name=quot;contentquot;
/> 











<layer
name=quot;decoratorquot;> 













<slot
name=quot;menuquot;
module=quot;Defaultquot;
action=quot;Menuquot;
/> 













<parameter
name=quot;directoryquot;>%core.template_dir%</parameter> 













<parameter
name=quot;templatequot;>Master</parameter> 











</layer> 









</layout> 









<layout
name=quot;slotquot;> 











<layer
name=quot;contentquot;
/> 









</layout> 







</layouts> 







<parameter
name=quot;http_headersquot;> 









<parameter
name=quot;Content‐Typequot;>text/html;
charset=UTF‐8</parameter> 







</parameter> 





</output_type>
    • The result: Menu Slot Content Layer
    • Nonsense Store Example
    • Search Engine Spam Route: <route 

name=quot;search_engine_spamquot; 

pattern=quot;^/products(/buy‐cheap‐{name:[Ss]+}‐at‐agavi‐dot‐org)?/(id:d+)$quot; 

module=quot;Defaultquot; 

action=quot;SearchEngineSpamquot; > 



<default
for=quot;namequot;>/buy‐cheap‐{nonsense}‐at‐agavi‐dot‐org</default> </route>
    • The Caching rules: <caching
method=quot;readquot;
lifetime=quot;2
hoursquot;> 

<group>products</group> 

<group
source=quot;request_parameterquot;>id</group> 

<group
source=quot;localequot;
/> 

 

<view>Success</view> 

 

<output_type
name=quot;htmlquot;> 



<layer
name=quot;contentquot;
/> 



<template_variable>title</template_variable> 

</output_type> </caching>
    • The Result: Restored from cache
    • Showcase Proof that it’s not just all theory.
    • Built by exozet interact, Berlin
    • The Chuckwalla A fat lizard from the Mojave desert (likely eats Agaves). Was a prototype IRC bot application with an IRC Context and a Web interface. IRC Routing implementation matched patterns against incoming messages. <configuration
context=quot;ircquot;> 

<!‐‐
so
someone
can
type
quot;!seen
Wombertquot;
on
IRC
‐‐> 

<route
pattern=quot;^!seen
(username:S+)$quot;
action=quot;LastSeenquot;
/> </configuration> <configuration
context=quot;webquot;> 

<!‐‐
on
the
web,
the
URL
is
/seen/Wombert
‐‐> 

<route
pattern=quot;^/seen/(username:S+)$quot;
action=quot;LastSeenquot;
/> </configuration>
    • !e End
    • Oh wait.
    • One More Thing... We have a new website! - Online today! - A lot of brand new tutorial and reference docs!
    • Questions?
    • Thank You for Attending! Shoot me an E-Mail: david.zuelke@bitextender.com Slides are available online at http://talks.wombert.de/ http://agavi.org/ is the Agavi website With Trac, Mailing Lists, PEAR Channel, Docs, ... Join the Agavi IRC channel: #agavi on irc.freenode.net