using zend framework with symfony

Download Using Zend Framework with Symfony

If you can't read please download the document

Upload: matthew-weier-ophinney

Post on 17-May-2015

27.311 views

Category:

Technology


2 download

DESCRIPTION

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

TRANSCRIPT

  • 1. Using Zend Framework with Symfony Matthew Weier O'Phinney Project Lead, Zend Framework

2. Matthew Weier O'Phinney

  • PHP Developer since 2000

3. Zend Framework contributor since 2006 4. Project Lead since 2009 5. Zend Framework: Some Background 6. Zend Framework is

  • a component library?

7. a full-stack framework? 8. Both. 9. 10. Comparing Symfony to Zend Framework 11. Why bother?

  • 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!) 14. Why use ZF with Symfony? 15. Take advantage of ZF features

  • 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) 20. Asynchronous Processing

  • In general: offloading processing to other processes
  • Queues, primarily

Examples:

  • Sending mail notifications

21. Interacting with 3 rdparty web services 22. Complex or expensive RDBMS operations May require a console runner or interaction with a message queue 23. Exposing Web Services

  • XML-RPC

24. SOAP 25. JSON-RPC 26. AMF 27. How do I use Zend Framework with Symfony? 28. Methods for integration

  • Typically, via a project or application plugin

29. classProjectConfigurationextendssfProjectConfiguration { 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... classProjectConfigurationextendssfProjectConfiguration { // ... // Autoload PEAR classes, too... static public function registerPear() { self::registerZend()->setFallbackAutoloader(); } } 31. Using Zend components: classreaderReadTaskextendssfBaseTask { // ... 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

  • 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

34. Feed tools

  • 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 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

  • Luceneis a binary format optimized for indexing documents and then retrieving documents by complex criteria.

40. Zend_Search_Luceneis binary-compatible with Lucene indexes

  • Supports search and retrieval from Lucene indexes

41. Supports generation of Lucene indexes 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 ( '%s ... (created %s)' , $hit ->uri, $hit ->synopsis, $hit ->timestamp ); } 44. PDF manipulation

  • PDF is an open specification produced by Adobe

45. Zend_Pdfoffers support for

  • Consuming and manipulating existing PDFs

46. Creating new PDF files 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?

  • Perform distinct, discrete processing tasks without the overhead of the full framework
  • 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.) 52. More reasons why

  • Unit test your domain model
  • If you're testing against a database, you're not unit testing

53. Typically less complex testing harnesses Reduce dependencies

  • Can reduce resource usage

54. Can be more performant (less bootstrapping and fewer resources often leads to faster code) Use in more environments 55. Domain Model roles

  • Entities often Plain Old PHP Objects

56. AggregatesorCollections 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. 59. Service Layer in perspective Data Access Objects and Data store(s) Data Mappers Entities Service Layer 60. Considerations

  • Service objects should return entities and/or aggregates

61. Consider returning Zend_Paginator objects

  • Countable

62. Iterable 63. Consumers do not need to understand implementation 64. More Considerations

  • Implement business/domain logic in the service layer
  • Validation/Filtering

65. Interactions between entities 66. ACLs 67. Back on track: Asynchronous Processing 68. Queues

  • Offload processing until later

69. Use cases

  • Send email later

70. Update indexes later 71. Interact with 3rd party APIs later (Salesforce, SugarCRM, etc) 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 ), " messages registered " ; 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

  • Self-documenting

75. Respond quickly 76. Correspond to existing service objects 77. Service types offered by ZF

  • AMF- Active Message Format, used byFlex 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-generationof WSDL and serialization to and from PHP objects 81. Basics

  • All servers follow PHP's SoapServer API
  • Instantiate

82. Addclasses and/or functions as callbacks 83. Handlethe request 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

  • Public methods are exposed by default

86. Define your public API in your service layer, and re-use however you need it

  • ACLs, validation, etc are already defined and encapsulated

87. Exposing Ajax Endpoints

  • XHR requests need to be fast
  • 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. Service layer objects, again, become your friend 89. Takeaways 90.

  • 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. 93. Resources

  • 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 96. Thank you!