Using Zend Framework with Symfony
Upcoming SlideShare
Loading in...5
×
 

Using Zend Framework with Symfony

on

  • 29,663 views

Details how and when to integrate Zend Framework components with Symfony, and provides an overview of several components particularly useful to Symfony developers.

Details how and when to integrate Zend Framework components with Symfony, and provides an overview of several components particularly useful to Symfony developers.

Statistics

Views

Total Views
29,663
Views on SlideShare
27,088
Embed Views
2,575

Actions

Likes
29
Downloads
400
Comments
1

41 Embeds 2,575

http://www.symfony.es 562
http://dicas-symfony.blogspot.com 338
http://symfony.com 334
http://www.symfonylab.com 301
http://www.symfony-project.org 229
http://dicas-symfony.blogspot.com.br 204
http://www.slideshare.net 146
http://swik.net 127
http://blog.dreamcss.com 118
http://dicas-symfony.blogspot.pt 53
http://it.blog.adclick.pt 51
http://www.phphatesme.com 35
http://dicas-symfony.blogspot.com.ar 9
http://dicas-symfony.blogspot.com.es 9
http://translate.googleusercontent.com 7
http://dicas-symfony.blogspot.fr 6
http://symfony.es 4
http://coderwall.com 4
http://dicas-symfony.blogspot.de 3
http://www.php-talks.com 3
http://dicas-symfony.blogspot.ro 3
http://dicas-symfony.blogspot.it 3
http://webcache.googleusercontent.com 3
http://www.sfexception.com 3
http://dicas-symfony.blogspot.co.il 2
http://pt.minichiello.name 2
http://feeds.feedburner.com 2
http://dicas-symfony.blogspot.ru 1
resource://brief-content 1
http://www.dicas-symfony.blogspot.com 1
http://static.slidesharecdn.com 1
http://siliconchaos.tumblr.com 1
http://www.techgig.com 1
http://dicas-symfony.blogspot.jp 1
http://dicas-symfony.blogspot.mx 1
http://icy:3000 1
http://www.phpframeworks.com 1
http://symfony.lab216.com 1
http://dicas-symfony.blogspot.in 1
http://www.mefeedia.com 1
http://www.thewebhatesme.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

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…
  • Zend Framework con Symfony, novedoso!!
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Using Zend Framework with Symfony Using Zend Framework with Symfony Presentation Transcript

  • Using Zend Framework with Symfony Matthew Weier O'Phinney Project Lead, Zend Framework
  • Matthew Weier O'Phinney
    • PHP Developer since 2000
    • Zend Framework contributor since 2006
    • Project Lead since 2009
  • Zend Framework: Some Background
  • Zend Framework is …
    • a component library?
    • a full-stack framework?
    • Both.
  •  
  • Comparing Symfony to Zend Framework
  • Why bother?
    • Both are solid frameworks
    • Both hit different “sweet spots” for developers
    • You can use them together (with Symfony Components, this goes both ways!)
  • Why use ZF with Symfony?
  • Take advantage of ZF features
    • Access to remote APIs (web services, feeds, etc.)
    • Support for Lucene indexes
    • PDF generation
    • Queueing
    • Cloud computing (storage, DBs, message queues)
  • Asynchronous Processing
    • In general: offloading processing to other processes
      • Queues, primarily
    • Examples:
      • Sending mail notifications
      • Interacting with 3 rd party web services
      • Complex or expensive RDBMS operations
    • May require a console runner or interaction with a message queue
  • Exposing Web Services
    • XML-RPC
    • SOAP
    • JSON-RPC
    • AMF
  • How do I use Zend Framework with Symfony?
  • Methods for integration
    • Typically, via a project or application plugin
  • class ProjectConfiguration extends sfProjectConfiguration { static protected $zendAutoloader = false; static public function registerZend() { if (!self:: $zendAutoloader ) { set_include_path( implode ( PATH_SEPARATOR, array ( sfConfig::get( 'sf_lib_dir' ) . '/vendor' , get_include_path(), ))) require_once 'Zend/Loader/Autoloader.php' ; self:: $zendAutoloader = Zend_Loader_Autoloader::getInstance(); } return self:: $zendAutoloader } }
  • Taking it further... class ProjectConfiguration extends sfProjectConfiguration { // ... // Autoload PEAR classes, too... static public function registerPear() { self::registerZend()->setFallbackAutoloader(); } }
  • Using Zend components: class readerReadTask extends sfBaseTask { // ... public function execute( $arguments = array (), $options = array () ) { // ... ProjectConfiguration::registerZend(); $feed = Zend_Feed_Reader::import( $feed ); // ... } } Just start using classes!
  • Some components you might want to use
  • Service components
    • Zend_Gdata
      • Contacts, calendars, and YouTube! Oh, my!
    • Zend_Service_Amazon
      • Book search, S3 and EC2, SQS (forthcoming)
    • Zend_Service_Akismet
      • Identify spam early
    • Many, many more
      • http://framework.zend.com/manual/en/zend.service.html
  • Feed tools
    • Zend_Feed_Reader – provides comprehensive support for consuming feeds, including RSS (1 and 2) and Atom
    • Zend_Feed_Writer – the mirror of Zend_Feed_Reader, aiding in construction of feeds
    • Zend_Feed_Pubsubhubbub – providing support for interacting with PuSH hubs, automating feed discovery and publication
  • $feed = Zend_Feed_Reader::import( $feedUri ); $metadata = array ( 'title' => $feed ->getTitle(), 'description' => $feed ->getDescription(), ); $entries = array (); foreach ( $feed as $entry ) { $entries [] = array ( 'title' => $entry ->getTitle(), 'link' => $entry ->getLink(), 'timestamp' => $entry ->getDateModified(), ); }
  • $feed = new Zend_Feed_Writer_Feed; $feed ->setTitle( $someTitle ) ->setLink( $url ) ->setDescription( $description ); $entry = $feed ->createEntry(); $entry ->setTitle( $entryTitle ) ->setLink( $entryLink ) ->setDateModified( time ()); $feed ->addEntry( $entry ); echo $feed ->export( 'atom' );
  • Lucene Indexes
    • Lucene is a binary format optimized for indexing documents and then retrieving documents by complex criteria.
    • Zend_Search_Lucene is binary-compatible with Lucene indexes
      • Supports search and retrieval from Lucene indexes
      • Supports generation of Lucene indexes
  • if ( is_dir ( $indexDir )) { $index = Zend_Search_Lucene::open( $indexDir ); } else { $index = Zend_Search_Lucene::create( $indexDir ); } $doc = new Zend_Search_Lucene_Document(); $doc ->addField(Zend_Search_Lucene_Field::Keyword( 'uri' , $url )); $doc ->addField(Zend_Search_Lucene_Field::UnIndexed( 'timestamp' , $ts )); $doc ->addField(Zend_Search_Lucene_Field::Text( 'synopsis' , $synopsis )); $doc ->addField(Zend_Search_Lucene_Field::Unstored( 'content' , $content )); $index ->addDocument( $doc );
  • $hits = $index ->find( '+hello -dolly' ); foreach ( $hits as $hit ) { printf ( '<a href=&quot;%s&quot;>%s ... (created %s)</a>' , $hit ->uri, $hit ->synopsis, $hit ->timestamp ); }
  • PDF manipulation
    • PDF is an open specification produced by Adobe
    • Zend_Pdf offers support for
      • Consuming and manipulating existing PDFs
      • Creating new PDF files
  • $pdf = Zend_Pdf::load( $pdfFile ); $page = $pdf ->pages[0]; $font = Zend_Pdf_Font::fontWithName( Zend_Pdf_Font::FONT_HELVETICA ); $page ->setFont( $font , 36) ->setFillColor( Zend_Pdf_Color_Html::color( '#CCC' )) ->drawText( 'U R H2O-marked' , 60, 500); $pdf ->save( $pdfFile );
  • Sidetrack: On Domain Models
  • Why?
    • Perform distinct, discrete processing tasks without the overhead of the full framework
      • When speed is necessary
      • When resource usage needs to be kept minimal
      • When performing processing that is non-web specific (console, RDBMS, communicating with web services, periodic tasks, etc.)
  • More reasons why
    • Unit test your domain model
      • If you're testing against a database, you're not unit testing
      • Typically less complex testing harnesses
    • Reduce dependencies
      • Can reduce resource usage
      • Can be more performant (less bootstrapping and fewer resources often leads to faster code)
    • Use in more environments
  • Domain Model roles
    • Entities – often Plain Old PHP Objects
    • Aggregates or Collections – of entities
    • Mappers – map entities to data persistence, and vice versa. Could be an ORM. Returns entities and aggregates.
    • Service layer objects – the public API for your domain model; uses other domain objects and performs business logic.
  • Service Layer in perspective Data Access Objects and Data store(s) Data Mappers Entities Service Layer
  • Considerations
    • Service objects should return entities and/or aggregates
    • Consider returning Zend_Paginator objects
      • Countable
      • Iterable
      • Consumers do not need to understand implementation
  • More Considerations
    • Implement business/domain logic in the service layer
      • Validation/Filtering
      • Interactions between entities
      • ACLs
  • Back on track: Asynchronous Processing
  • Queues
    • Offload processing until later
    • Use cases
      • Send email later
      • Update indexes later
      • Interact with 3rd party APIs later (Salesforce, SugarCRM, etc)
  • $queue = new Zend_Queue( 'MemcacheQ' , array ( 'name' => 'my-uber-queue' , 'host' => 'queue.host.tld' , )); $queue -> send ( 'Some message' ); $messages = $queue ->receive(5); echo count ( $queue ), &quot; messages registered &quot; ; foreach ( $messages as $message ) { // do something with message, // typically $message->body $queue ->deleteMessage( $message ); }i
  • Exposing Web Services
  • Goals of good web services
    • Self-documenting
    • Respond quickly
    • Correspond to existing service objects
  • Service types offered by ZF
    • AMF - Active Message Format, used by Flex and Flash
    • JSON-RPC - RPC using JSON as the serialization format; clients in Dojo, YUI and ExtJS
    • XML-RPC - Standard protocol used everywhere
    • SOAP - with support for auto-generation of WSDL and serialization to and from PHP objects
  • Basics
    • All servers follow PHP's SoapServer API
      • Instantiate
      • Add classes and/or functions as callbacks
      • Handle the request
  • $server = new Zend_XmlRpc_Server(); $server ->setClass( 'My_Service_ApiClass' , 'api' ); echo $server ->handle(); if ( $_SERVER [ 'REQUEST_METHOD' ] == 'GET' ) { $server = new Zend_Soap_AutoDiscover(); } else { $server = new Zend_Soap_Server( $thisScript ); } $server ->setClass( 'My_Service_ApiClass' ); $server ->handle();
  • Service Layer Objects are King
    • Public methods are exposed by default
    • Define your public API in your service layer, and re-use however you need it
      • ACLs, validation, etc are already defined and encapsulated
  • Exposing Ajax Endpoints
    • XHR requests need to be fast
      • If you are doing RPC-style requests, Zend_Json_Server is your friend
      • JSON-REST is becoming popular; consider exposing an alternate lightweight RESTful MVC that consumes your service objects, or Symfony's REST API.
    • Service layer objects, again, become your friend
  • Takeaways
    • Zend Framework can provide a wealth of functionality you can draw on in your Symfony applications.
    • Consider writing a rich domain model separate from your application that draws on components from a variety of sources.
    • Offload processing when you can, and setup service endpoints outside your main web application for performance reasons.
  • Resources
    • The ZF Manual: http://framework.zend.com/manual
    • My slides on Slideshare http://slideshare.net/weierophinney
    • Rate this talk: http://joind.in/1413
  • Thank you!