www.it-ebooks.info Magento PHP Developer's Guide Get started with the flexible and powerful e-commerce framework, Magento Allan MacGregor BIRMINGHAM - MUMBAI www.it-ebooks.info Magento PHP Developer's Guide Copyright © 2013 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: April 2013 Production Reference: 1250313 Published by Packt Publishing Ltd Livery Place 35 Livery Street Birmingham B3 2PB, UK ISBN 978-1-78216-306-0 www.packtpub.com Cover Image by Sandeep Babu (sandyjb@gmail.com) www.it-ebooks.info Credits Author Project Coordinator Allan MacGregor Anugya Khurana Reviewers Proofreader Jay El-Kaake Jonathan Todd Michael K Kreitzer Indexer Corey Slavnik Tejal Soni Acquisition Editor Graphics Edward Gordon Aditi Gajjar Lead Technical Editor Azharuddin Sheikh Technical Editors Production Coordinator Arvindkumar Gupta Cover Work Devdutt Kulkarni Kaustubh S Mayekar Arvindkumar Gupta Ankita R Meshram www.it-ebooks.info About the Author Allan MacGregor is a Magento Certified Developer Plus with four years of Magento experience He also has a certification in Linux System Administration by IBM He started working with Magento as a freelance looking for a better framework to build e-commerce solutions, and he is now the Magento Lead Developer at Demac Media (www.demacmedia.com) He's very passionate about software development in general He is constantly working with new technologies and frameworks At Demac Media, he has participated in building core solutions for a wide range of clients; this has given him the experience and knowledge to solve many Magento challenges As part of an internal project at Demac Media, he worked on Triplecheck.io (www.triplecheck.io), a unique service for monitoring and auditing the code health of a Magento store You can also follow him on Twitter at http://www.twitter.com/allanmacgregor Writing this book has been a demanding experience but full of rewards While writing, I have discovered more of Magento and a little bit more of myself, both as a person and a developer First and foremost, I want to thank my amazing wife for her unconditional support and understanding while working on these projects To Matthew Bertulli and Dimitri Colomvakos, co-founders of Demac Media, for all their support To my co-workers and friends, Michael Krietzer and Corey Slavnik, who kindly volunteered their time and effort to review this book And special thanks to the whole Demac Media family This wouldn't be possible without any of you www.it-ebooks.info About the Reviewers Jay El-Kaake started his first tech entrepreneurship adventure at the age of 10 when he developed and launched his first website Now, as a Co-founder and CEO of Sweet Tooth Inc, Jay oversees up to 1,000 Magento stores per year as Sweet Tooth's client base of over 3,000 Magento e-commerce clients grows Through his role at Sweet Tooth Inc., Jay was the lead in developing some of the most popular extensions of Magento: Sweet Tooth Rewards, a full-featured loyalty and rewards program extension; Better Store Search, a turn-key product search optimizer; Better Store CMS, a Magento CMS improver; and Enhanced Grid, the second most downloaded community-written extension for Magento with over 70,000 installations Jay's GitHub account can be found at https://github.com/jayelkaake You can also follow Jay on Twitter at http://www.twitter.com/jayelkaake Michael K Kreitzer, born in Chatham, Ontario, and raised in the small town of Thamesville, began his journey in the IT industry at an early age from the moment he wrote his first Hello World program He studied Computer Programming at Sheridan College in Oakville, Ontario and is now a Magento Certified Plus Developer at Demac Media Inc He lives in Mississauga, Ontario with his wife Megan and dog Padme I would like to thank my family for their love and support, as well as my co-workers who help me continue to learn every day www.it-ebooks.info Corey Slavnik is a Certified Magento Developer from Toronto, Ontario He understood his affinity for programming at a young age when he built games in RPG Maker 2000 He attended McMaster University for his undergraduate degree and continued to learn (and love) Magento at Demac Media Corey also enjoys craft beer and yoga I would like to thank my parents for always pushing me to pursue great opportunities, and my co-workers, who have helped me learn all things about Magento www.it-ebooks.info www.PacktPub.com Support files, eBooks, discount offers and more You might want to visit www.PacktPub.com for support files and downloads related to your book Did you know that Packt offers eBook versions of every book published, with PDF and ePub files 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 www.it-ebooks.info www.it-ebooks.info Table of Contents Preface 1 Chapter 1: Understanding and Setting Up Our Development Environment LAMP from scratch Getting VirtualBox Booting our virtual machine Installing Apache2 Installing PHP Installing MySQL Putting everything together Up and running with Vagrant Installing Vagrant Choosing an IDE Working with a version control system Summary Chapter 2: Magento Fundamentals for Developers 11 16 16 17 18 21 22 24 24 25 27 Zend Framework – the base of Magento 27 Magento folder structure 29 Modular architecture 30 Autoloader 30 Code pools 32 Routing and request flow 33 Magento version of MVC 38 Models 42 Views 43 Dissecting a layout file 43 Controllers 46 www.it-ebooks.info Appendix public function indexAction() { echo ‘Hello World this is the default action’; } } Testing the route Now that we have created our router and controller, we can test it out by opening http://magento.localhost.com/hello/index/index, for which we should see the following screenshot: By default, Magento will use both the index controller and the index action as defaults for each extension So, if we go to http://magento.localhost.com/ hello/, we should see the same screen To conclude our introduction to Magento Module, let’s add a new route to our controller: Navigate to the extension root directory Open IndexController.php [ 229 ] www.it-ebooks.info Hello Magento Copy the following code (the file location is app/code/local/Mdg/Hello/ controllers/IndexController.php):