Extending Symfony2 Web Application Framework Optimize, audit, and customize web applications with Symfony Sbastien Armand
BIRMINGHAM - MUMBAI Extending Symfony2 Web Application Framework Copyright 2014 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information. First published: March 2014 Production Reference: 1180314 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78328-719-2 www.packtpub.com Cover Image by Suman Kumar ( sumankumarsinha@yahoo.com ) Credits Author Project Coordinator Sbastien Armand Jomin Varghese Reviewers Proofreaders Vincent Composieux Simran Bhogal Boris Gury Maria Gould Eric Pidoux Adam Prager Indexers Mariammal Chettiyar Acquisition Editors Monica Ajmera Mehta Rebecca Pedley Antony Lowe Graphics Yuvraj Mannari Content Development Editor Rebecca Pedley Production Coordinator Manu Joseph Technical Editors Menza Mathew Cover Work Shali Sasidharan Manu Joseph Copy Editors Alfda Paiva Karuna Narayanan About the Author Sbastien Armand is a software developer based in Beijing, China. He spent most of the past fve years working with Symfony, building internal IT systems. He co-founded mashupsports.com , a social website for sports enthusiasts based on Symfony2.
He contributed to Symfony and the Symfony documentation on many occasions. I would like to thank my ever-loving and understanding wife for all her support. If it weren't for her, I would have never started this book. Thank you. I'll be home for breakfast from now on! Also my parents and sister for just being awesome. Of course, I also extend my thanks to the whole Symfony community.
It feels great being a part of it! About the Reviewers Vincent Composieux is a French PHP developer based in Paris and working at Ekino. Previously, he worked for e-commerce companies and web agencies on multiple great web projects with high traffc. He loves web technologies and frameworks and has experience in using Zend Framework, Magento, and now Symfony. He has had great experience in Symfony because he has used it since the very frst version and is actively involved in the Symfony community. He has even developed some bundles such as FeedBundle for managing the RSS and Atom feeds and some others. He is also a contributor on the Sonata bundles suite.
You can learn more about him and contact him on his personal website via http://vincent.composieux.fr . Boris Gury is the CTO of Azurgate SA. He is a French startup editor and has edited the well-known French mobile application: Se Coucher Moins Bte. He is also a proud member of The Big Brains Company. He has been active on the Web since 1997, and has been using computers since he was four; he likes beer as well as software architecture and best practices. He is passionate of R&D yet pragmatic.
He works mainly in PHP using Symfony2, but still picks anything that does the job (Python, Bash, C, and Ruby). He has developed a real expertise in implementing scalable applications on high-load applications. I would like to thank all my friends, with a special mention to all the members of The Big Brains Company. My deep gratitude goes to my parents as well. Eric Pidoux has a master's degree in Computer Science from Miage Aix-Marseille and is currently working as a Lead Web Developer at Createur.ch (Lausanne, Switzerland), working especially on Symfony2 framework and PHP5 websites. He started working as a Java and PHP developer and dropped the Java skill to learn Symfony and then become a Symfony2 expert.
He already worked as a technical reviewer on GitLab Repository Management , J.M. Hethey , Packt Publishing . Adam Prager is a full stack web application developer who has created many data-heavy business management applications in the areas of Customer Relationship Management, Enterprise Resource Planning, and Laboratory Information Management. He is a frm believer in the value and power of open source software, and contributes to projects such as Doctrine and Symfony regularly on GitHub. He has published numerous Symfony bundles and jQuery plugins of his own. Adam currently works for Netlife in Hungary.
Netlife is a consulting and IT services company that provides web application development services using the latest technologies, and complete business solutions based on SAP consulting. As a diverse end-to-end IT solutions provider, Netlife offers a range of expertise aimed at assisting customers to compete successfully in the ever-changing IT industry. It provides long-term solutions with a focus on quality. They have excellent domain expertise in SAP CRM, custom web application development, and user experience design. www.PacktPub.com Support fles, eBooks, discount offers, and more You might want to visit www.PacktPub.com for support fles and downloads related to your book. Did you know that Packt offers eBook versions of every book published, with PDF and ePub fles available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy.
Get in touch with us at service@packtpub.com for more details. At www.PacktPub.com , you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. Why subscribe? Fully searchable across every book published by Packt Copy and paste, print and bookmark content On demand and accessible via web browser Free access for Packt account holders If you have an account with Packt at www.PacktPub.com , you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.
Table of Contents Services 5 A geolocation service 6 Testing services and testing with services 12 Tagging services 14 Listeners 18 Updating user preferences using custom events 20 Improving user performance 24 Code that works after the response 24 Summary 26 Commands 27 The initial situation 27 Resizing user pictures 28 Testing a command 31 Commands as an interface to services 32 Twig 35 Managing our scripts 36 Testing a Twig extension 38 The time difference flter Summary 40 An input for geographical coordinates 41 Setting up the basics 43 Using the map 46 Data transformers 48 Forms based on user data 51 Going further 53 The initial setup 53 Adding and removing felds 55 Summary 58 Authentication 59 Simple OAuth with GitHub 60 The frewall 61 The security factory 64 Authorization 71 Voters 72 Annotations 77 Defning an annotation 78 Securing controllers with custom annotations 81 Securing an API an example 84 Summary 86 Creating your own data types 87 User and meetup locations 87 Testing 90 Custom DQL functions 92 Versioning 96 Setting a version on all entities 98 Using and updating versions 100 Testing 101 Creating a Doctrine flter 102 Summary 106 Creating the bundle 107 Exposing the confguration 110 Getting ready to share 117 Research 117 Documentation 117 Testing 118 Distribution and licensing 119 Is it just a bundle? 120 Summary 121 Preface The frst stable version of Symfony2 was released more than two years back. Coming from all the experience acquired from Symfony1, the promise was to remove all the magic and provide a solid and modular basis to build web applications. The trade-off's inconvenience was justifed in order for developers to regain full control and knowledge of the working of their application. To achieve this, it was decided that everything would be a bundle. The core framework itself is just a collection of bundles, which is everything you need to get started. This great architecture being at the heart of Symfony2 and the promise of greater modularity and control over the whole framework enables any developer to create their own extensions.