Using Zend Framework with Symfony

35,779 views

Published on

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

Published in: Technology
1 Comment
29 Likes
Statistics
Notes
  • Zend Framework con Symfony, novedoso!!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
35,779
On SlideShare
0
From Embeds
0
Number of Embeds
2,713
Actions
Shares
0
Downloads
408
Comments
1
Likes
29
Embeds 0
No embeds

No notes for slide

Using Zend Framework with Symfony

  1. Using Zend Framework with Symfony Matthew Weier O'Phinney Project Lead, Zend Framework
  2. Matthew Weier O'Phinney <ul><li>PHP Developer since 2000
  3. Zend Framework contributor since 2006
  4. Project Lead since 2009 </li></ul>
  5. Zend Framework: Some Background
  6. Zend Framework is … <ul><li>a component library?
  7. a full-stack framework?
  8. Both. </li></ul>
  9.  
  10. Comparing Symfony to Zend Framework
  11. Why bother? <ul><li>Both are solid frameworks
  12. Both hit different “sweet spots” for developers
  13. You can use them together (with Symfony Components, this goes both ways!) </li></ul>
  14. Why use ZF with Symfony?
  15. Take advantage of ZF features <ul><li>Access to remote APIs (web services, feeds, etc.)
  16. Support for Lucene indexes
  17. PDF generation
  18. Queueing
  19. Cloud computing (storage, DBs, message queues) </li></ul>
  20. Asynchronous Processing <ul><li>In general: offloading processing to other processes </li><ul><li>Queues, primarily </li></ul><li>Examples: </li><ul><li>Sending mail notifications
  21. Interacting with 3 rd party web services
  22. Complex or expensive RDBMS operations </li></ul><li>May require a console runner or interaction with a message queue </li></ul>
  23. Exposing Web Services <ul><li>XML-RPC
  24. SOAP
  25. JSON-RPC
  26. AMF </li></ul>
  27. How do I use Zend Framework with Symfony?
  28. Methods for integration <ul><li>Typically, via a project or application plugin </li></ul>
  29. 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 } }
  30. Taking it further... class ProjectConfiguration extends sfProjectConfiguration { // ... // Autoload PEAR classes, too... static public function registerPear() { self::registerZend()->setFallbackAutoloader(); } }
  31. 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!
  32. Some components you might want to use
  33. Service components <ul><li>Zend_Gdata </li><ul><li>Contacts, calendars, and YouTube! Oh, my! </li></ul><li>Zend_Service_Amazon </li><ul><li>Book search, S3 and EC2, SQS (forthcoming) </li></ul><li>Zend_Service_Akismet </li><ul><li>Identify spam early </li></ul><li>Many, many more </li><ul><li>http://framework.zend.com/manual/en/zend.service.html </li></ul></ul>
  34. Feed tools <ul><li>Zend_Feed_Reader – provides comprehensive support for consuming feeds, including RSS (1 and 2) and Atom
  35. Zend_Feed_Writer – the mirror of Zend_Feed_Reader, aiding in construction of feeds
  36. Zend_Feed_Pubsubhubbub – providing support for interacting with PuSH hubs, automating feed discovery and publication </li></ul>
  37. $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(), ); }
  38. $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' );
  39. Lucene Indexes <ul><li>Lucene is a binary format optimized for indexing documents and then retrieving documents by complex criteria.
  40. Zend_Search_Lucene is binary-compatible with Lucene indexes </li><ul><li>Supports search and retrieval from Lucene indexes
  41. Supports generation of Lucene indexes </li></ul></ul>
  42. 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 );
  43. $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 ); }
  44. PDF manipulation <ul><li>PDF is an open specification produced by Adobe
  45. Zend_Pdf offers support for </li><ul><li>Consuming and manipulating existing PDFs
  46. Creating new PDF files </li></ul></ul>
  47. $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 );
  48. Sidetrack: On Domain Models
  49. Why? <ul><li>Perform distinct, discrete processing tasks without the overhead of the full framework </li><ul><li>When speed is necessary
  50. When resource usage needs to be kept minimal
  51. When performing processing that is non-web specific (console, RDBMS, communicating with web services, periodic tasks, etc.) </li></ul></ul>
  52. More reasons why <ul><li>Unit test your domain model </li><ul><li>If you're testing against a database, you're not unit testing
  53. Typically less complex testing harnesses </li></ul><li>Reduce dependencies </li><ul><li>Can reduce resource usage
  54. Can be more performant (less bootstrapping and fewer resources often leads to faster code) </li></ul><li>Use in more environments </li></ul>
  55. Domain Model roles <ul><li>Entities – often Plain Old PHP Objects
  56. Aggregates or Collections – of entities
  57. Mappers – map entities to data persistence, and vice versa. Could be an ORM. Returns entities and aggregates.
  58. Service layer objects – the public API for your domain model; uses other domain objects and performs business logic. </li></ul>
  59. Service Layer in perspective Data Access Objects and Data store(s) Data Mappers Entities Service Layer
  60. Considerations <ul><li>Service objects should return entities and/or aggregates
  61. Consider returning Zend_Paginator objects </li><ul><li>Countable
  62. Iterable
  63. Consumers do not need to understand implementation </li></ul></ul>
  64. More Considerations <ul><li>Implement business/domain logic in the service layer </li><ul><li>Validation/Filtering
  65. Interactions between entities
  66. ACLs </li></ul></ul>
  67. Back on track: Asynchronous Processing
  68. Queues <ul><li>Offload processing until later
  69. Use cases </li><ul><li>Send email later
  70. Update indexes later
  71. Interact with 3rd party APIs later (Salesforce, SugarCRM, etc) </li></ul></ul>
  72. $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
  73. Exposing Web Services
  74. Goals of good web services <ul><li>Self-documenting
  75. Respond quickly
  76. Correspond to existing service objects </li></ul>
  77. Service types offered by ZF <ul><li>AMF - Active Message Format, used by Flex and Flash
  78. JSON-RPC - RPC using JSON as the serialization format; clients in Dojo, YUI and ExtJS
  79. XML-RPC - Standard protocol used everywhere
  80. SOAP - with support for auto-generation of WSDL and serialization to and from PHP objects </li></ul>
  81. Basics <ul><li>All servers follow PHP's SoapServer API </li><ul><li>Instantiate
  82. Add classes and/or functions as callbacks
  83. Handle the request </li></ul></ul>
  84. $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();
  85. Service Layer Objects are King <ul><li>Public methods are exposed by default
  86. Define your public API in your service layer, and re-use however you need it </li><ul><li>ACLs, validation, etc are already defined and encapsulated </li></ul></ul>
  87. Exposing Ajax Endpoints <ul><li>XHR requests need to be fast </li><ul><li>If you are doing RPC-style requests, Zend_Json_Server is your friend
  88. JSON-REST is becoming popular; consider exposing an alternate lightweight RESTful MVC that consumes your service objects, or Symfony's REST API. </li></ul><li>Service layer objects, again, become your friend </li></ul>
  89. Takeaways
  90. <ul><li>Zend Framework can provide a wealth of functionality you can draw on in your Symfony applications.
  91. Consider writing a rich domain model separate from your application that draws on components from a variety of sources.
  92. Offload processing when you can, and setup service endpoints outside your main web application for performance reasons. </li></ul>
  93. Resources <ul><li>The ZF Manual: http://framework.zend.com/manual
  94. My slides on Slideshare http://slideshare.net/weierophinney
  95. Rate this talk: http://joind.in/1413 </li></ul>
  96. Thank you!

×