More Related Content


php[world] Magento101

  1. Magento 101 Getting Started with Magento Development Who you are Magento 101 Getting Started with Magento Development Crash Course For PHP Developers Mathew Beane November 17th 2015
  2. 2 Mathew Beane @aepod Director of Systems Engineering - Robofirm Zend Z-Team Volunteer – Magento Division Family member – 3 Kids and a Wife Magento Certified Developer
  3. • Open-source PHP E-Commerce Market Leader • Large community • Full Featured E-Commerce Platform • Incredibly Flexible • Highly Structured • Steep learning curve because of complexity • Enterprise Edition offers support and more features So its not a color, super villain or an electrical generator? Magento is an open-source content management system for e-commerce web sites. - Wikipedia Image: Marvel Comics What is Magento?
  4. Todays tl;dr The Magento application Common toolsets for Magento developers Quick start Magento development • Brief overview • Install guide • Backend development • Frontend development About training & certification Magento 2 4
  5. Magento 101 – Mathew Beane – php[world] 2015 Magento Application Core Overview – Directories and Designs 5
  6. Magento 1 Community Edition (CE): Enterprise Edition (EE): Still primarily used on live sites. Magento 2 2.0.0 Just Released, ready for use. Full refactor, modern OO concepts. Todays Magento
  7. Community member John Knowles produced this amazing patch guide. patches-should-i-update-my-version-magento Scan for missing patches using the byte tool: Over the last year there has been a serious uptick in hacker activity aimed at Magento. Do not be caught unaware, patch today. Patch Today!
  8. Magento Design Patterns Selected Design Patterns Model View Controller Pattern Business logic happens in the models and the controller maps the model-data to the views. The block system helps with rendering because Magento is heavy on the Model/View side. Front Controller Pattern Magento uses an index.php which has a single point of entry Mage::app() which initializes the environment and routes the request to the controller. Factory Pattern Heavily used by Magento, a good example is the getModel() function which accesses models via aliases from the configuration XML. Module Pattern Magento relies on a very modular architecture, however a lot of functionality breaks this due to heavy usage of the Mage core class throughout the application. More reading:
  9. Magento Core Code Everything in the Magento zip file or installed by Magento should be considered core code. Partial List of Core Files • index.php • app/code/core • app/design/[adminhtml,frontend]/[base,default] • js/jslib_installed_with_magento/ • lib/library_installed_with_magento/ • skin/[adminhtml,frontend]/[base,default] For a complete list, download current Magento and look through the code.
  10. How to Keep Core Clean Source: • Magento allows class overrides on nearly everything. • Magento’s Event/Observer mechanism is pre- built into most business logic. You can add more if you need. • Designs/Templates have a fallback system that allows you to utilize any of the core layouts and templates, or replace them in your current design. • Untouched index.php. Outside of development and very rare implementations you should not “need” to edit this.
  11. Core Changes That Are OK! • Patches: Magento releases patches that are not controlled via composer or version number. • Updating Shared Libraries: For instance CM_Redis, or a payment gateway that are included with core Magento. On your local Copy: Careful not to commit any changes from core, employ a .gitignore strategy. • Changing core to learn about Magento is OK • Changing core to debug is OK.
  12. Magento Core Directory Overview Path Usage /app/ Where most of the PHP resides /js/ External Javascript library. Typically only used by extensions that are including common assets. /lib/ “External code library” With lots of core code, this can also contain 3rd party frameworks and other goodies. /skin/ CSS, Images and Javascript /var/ Contains logs, reports, session files and a variety of other goodies. Path Usage /app/code/community Downloaded modules built by Magento 3rd party developers. /app/code/core/ Core modules, out of the box Magento, do not touch. /app/code/local/ Modules built for the specific application/store. Path Usage /app/etc/local.xml Master configuration file for Magento, contains database, cache and other settings. /app/etc/config.xml Default settings, with some directory locations. Usually this is not touched. app/etc/modules/ Modules loading configuration files. / - Directory Root /app/code/ /app/etc/
  13. Magento Core Design Directories Path Usage /app/design/frontend/base/ Default Templates, falls back from any package. /app/design/frontend/default/ Old pre-rwd base theme default package. /app/design/frontend/rwd/ RWD is a responsive template package. Path Usage /skin/frontend/default/default/css Default theme CSS files /skin/frontend/default/default/images Default theme image files. /skin/frontend/default/default/js Default theme javascript files /app/design/frontend /skin/frontend/
  14. Magento Core Designs Pattern Frontend design directories fall into this pattern: app/design/<area>/<package>/<theme>/[css,images,js] skin/<area>/<package>/<theme>/[css,images,js] • Area: Typically frontend or adminhtml. • Package / Theme: Accommodates Magento’s theme fallback logic.
  15. Package and Theme Fallback Logic Graph from: Package: custom_package Theme: custom_theme Never edit base/default, or default/default. These should be considered core code. (Extensions also install in here) When creating a new design use the override system creating a custom_package and custom_theme.
  16. Magento 101 – Mathew Beane – php[world] 2015 Magento Application Core Overview – Configuration and Database 16
  17. Magento Configuration Structure • XML Based • xpaths are used as keys in core_config_data table in mysql • Hierarchical with overrides and scope • In the app you can call any final config value using the xpath • Used for configuration values (cached via Configuration Cache) • Also used for layout rules (cached via Layout Cache)
  18. Magento Configuration Scope • Global Scope • Default Values • Used if not found in scope • Website Scope • Website scope is used to override global scope per website. • Used to support Multi-Store environments • View Scope (subset of website scope) • Used for currency, tax, and language settings • Typically shown as a dropdown for Language/Currency • Limited Configuration Values Available Magento Admin – Choosing Configuration Scope
  19. Magento Database Interface Magento uses a basic CRUD Model There are three basic components to the data interface Model: Doesn’t contain database code Resource Model: Read/Write adapters for communicating to database Collection: PHP Object used to hold model instances, implements several PHP Standard Library interfaces to work with the collection. “Most Magento Models can be categorized in one of two ways. There's a basic, ActiveRecord-like/one-object-one-table Model, and there's also an Entity Attribute Value (EAV) Model.“ - Magento’s CRUD Resource Model: Mage_Core_Model_Abstract • Create & Update: save() • Read: load() • Delete: delete()
  20. Magento Database Structure • Most of the tables are InnoDB • Several Hundred Tables • The DB can grow very large due to logging, carts, and other data • Typically not edited by hand in any way at any time. Selected Tablespace Areas: core_ : Many of the core system settings tables reside here. catalog_ : Stores products, categories, associations, and other data. customer_ : Stores the customer information sales_ : Quotes, invoices and orders, and related data Image Source:
  21. Magento 101 – Mathew Beane – php[world] 2015 Magento Common Toolsets Overview & Best Practices 21
  22. Magento Development IDE Choices PHPStorm with Magicento Plugin PHPStorm with the Magicento is also very popular. Zend Studio Built in support for Magento, built on eclipse and a very nice platform for editing Magento.
  23. Magento Server Stack Tools • Magento does well on the Zend Server platform. Zend provides great PHP support. It has Z- Ray, tools for deployment, monitoring and other features which make it very nice in production. • Newrelic: Magento even has an official plugin to send data to newrelic. • : Used for tracing code, like newrelic + xhprof. • Z-Ray: Standalone works very well with the Magento plugin, add to apache/nginx stack.
  24. Z-Ray and the Magento Plugin • Full Database Queries • Debug Production & Mobile Devices with Z-Ray Live • Fully Extensible – Framework Support • All Function calls – with details • Errors and Warnings • Magento Plugin Provides: • Detailed Overview • Events / Observers • Layouts / Blocks • Logs / Modules • Details, details… details!
  25. Magento Application Stack Tools N98-magerun: A must have CLI tool, the swiss army knife for Magento Developers. Alan Storm’s Commerce Bug: This thing is amazing, best $50 I ever spent on Magento. N98-magerun in action. Don’t miss Alan Storms Blog:
  26. Magento 101 – Mathew Beane – php[world] 2015 Magento Quick Start Getting Started With Magento Development 26
  27. Magento Quick Start - Itinerary • Installation: Manual and automated methods. Revision control and workflow. • Development: The basic parts of an extension. • Theming and Designs: Frontend development for Magento. MTG Card c/o Wizards of the Coast
  28. Magento Quick Start – “The App” Designs Layouts: XML Configurations Templates: PHTML templates Static Files: CSS, JS and Images Extensions Blocks: Template Business Logic Controllers: Request Routing Helpers: General Functions Models: Data and Database Interface Magento Extension Developers Guide Figure 3: MVC design pattern for Magento
  29. Magento System Requirements Operating System Linux x86-64 Web Server Apache 2.x Nginx 1.7.x Database MySQL 5.6 (Oracle or Percona) PHP PHP 5.4 – 5.5 • Optional Services Redis Memcache Apache SOLR • Required PHP Extensions CURL DOM gd hash Iconv mcrypt pcre pdo pdo_mysql simplexml
  30. Magento 101 – Mathew Beane – php[world] 2015 Magento Quick Start Installation Guide 30
  31. Magento Manual Installation Installing Magento from scratch is really only a couple basic steps. 1.Create database, database user and proper grants. 2.Copy source files into the webroot. (Optional) Add sample data to database, and media to webroot/media 3.Set and confirm permissions on all files for webserver. 4.Point web browser at your webroot. 5.Step through the installer. 6.Do any post install tasks, because your done.
  32. Magento Automated Installation • Name says it all, no puppet or chef. Virtualbox + Vagrant. Magento with sample data. A little stale. • A more active and configurable vagrant install. Virtualbox, Vagrant and Chef. Magento with sample data. • Ready to go docker container, you will have to do a little configuration to make this one work. Magento, no sample data.
  33. Magento Loves Composer Manage all your extensions, designs, and other stuff via composer. Manage core install with this community package. It makes it easy to update your core if you require it using composer. A community repository of a lot of the common extensions that are used by a lot of Magento developers.
  34. Keeping Magento Clean Using Git • git is really required for managing a Magento installation. • Use symbolic links and .gitignore for volatile directories and sensitive files. • When used with composer gitignore will require a more complex strategy. • Checkout Fabrizo Branca’s presentations on this for a very in depth study: • Sonassi hass another great guide:
  35. Magento 101 – Mathew Beane – php[world] 2015 Magento Quick Start Development Guide 35
  36. Magento Extension Structure Extension / Module Code Configuration: Module configuration and status values Blocks: Classes for interfacing with phtml templates Controllers: Classes that handle request routing Helpers: General functions and helpers Models: Resource Models and Collections for DB Installers/Upgrades: Install, upgrade and even remove extension data
  37. Magento Extensions - Configuration Module Status /app/etc/modules/demo_example.xml <?xml version="1.0"?> <config> <modules> <Demo_Example> <active>true</active> <codePool>community</codePool> </Demo_Example> </modules> </config> Module Config /app/code/community/Demo/Example/etc/config.xml • Merged into with the rest of the configuration • Defines all models, blocks and classes for the module • Similar to the app/etc/local.xml or data in the core_config_data table • Controls installation and updates of module data
  38. Magento Extensions - Designs • Configuration Files: /app/etc/modules/demo_example.xml Definitions for Blocks, Layouts and Assets should be declared here. • Blocks: app/etc/code/community/Demo/Example/Blocks Classes for interfacing with the templates. • Layouts: app/design/frontend/base/default/layout/demo_example.xml XML Configurations for Blocks. • Templates: app/design/frontend/base/default/template/demo/template.phtml XML Configurations for Blocks. • Media: skin/frontend/base/default/[css,images,js]/demo/filename Extension specific media assets
  39. Magento Extensions - Code • Controller: /app/code/community/Demo/Example/controllers/Router.php Dispatched through Front Controller object. Action is matched to class and method. Controller instances Layout Object which in turn calls Block classes. • Helper: /app/code/community/Demo/Example/Helper/Data.php Used whenever you either are too lazy to make a model, or it just doesn’t make sense in the context of a model. (Does it have an internal state – It’s a model) • Model: /app/code/community/Demo/Example/Model/Thing.php Used to model data structures. Oftentimes calling database through Resource Models. Resource Models wrap all database transactions. Additionally Magento uses collections to create and iterate through lists of Models.
  40. Using Core Class Overrides Easy and “Safe” way to override core functionality Files are placed in: /app/code/local/Mage/Catalog/Block/Navigation.php This will override and replace /app/code/core/Mage/Catalog/Block/Navigation.php • Copy the file from the original and edit as you would expect. • Use this method very sparingly. • It creates technical debt, because you have to maintain your “copy” of the core file. • Not the preferred method and some still consider this editing core.
  41. Extensions - Rewriting Core Classes • Still easy and a “Safer” way to override core functionality • Requires an extension to create a class rewrite • Rewrite class extends core class • Preserves code through upgrade • Much safer than just overriding the whole class because its isolated in an extension. • Chain rewrites together to create complex dependencies • Winner takes all strategy makes this bothersome as site grows. /app/code/community/Demo/Example/etc/config.xml <config> <global> <models> <catalog> <rewrite> <product>Demo_Example_Model_Product</product> </rewrite> </catalog> </models> </global> </config> /app/code/community/Demo/Example/Product.php <?php include(‘Mage/Catalog/Model/Product.php’); class Demo_Example_Model_Product extends Mage_Catalog_Model_Product{ // Functions here will rewrite the Magento Catalog Product Model Class }
  42. Extensions - Events / Observers • The proper way to hook into Magento Business Logic • Declared in config.xml • Many Observers to One Event • Observers are Models in the modules Model/ directory • Events are declared for most things you will need to hook into • You can add your own Events, combining this with the Rewrite or Override methods /app/code/community/Demo/Example/etc/config.xml <config> <global> <events> <checkout_cart_product_add_after> <observers> <Demo_Example_Model_Observer> <type>singleton</type> <class>Demo_Example_Model_Observer</type> <method>addtocartEvent</method> </Demo_Example_Model_Observer> </observers > </checkout_cart_product_add_after> </events> </global> </config> /app/code/community/Demo/Example/Model/Observer.php class Demo_Example_Model_Observer { public function addtoCartEvent(Varien_Event_Observer $observer){ // Observer Code goes here } } List of events in CE 1.9
  43. Extensions - Blocks • Blocks are PHP Objects • Each block is tied to a single Template file. • Inside the Template, $this keyword refers to the Template’s Block object. • Blocks can include Blocks nesting them utilizing the getChildHtml() method. • Blocks are instanced from the Layout configuration, typically from layout XML files. /app/code/community/Demo/Example/etc/config.xml <config> <global> <blocks> <exampleblock> <class>Demo_Example_Block</class> </exampleblock> </blocks> </global> </config> /app/code/community/Demo/Example/Block/Product.php <?php class Demo_Example_Block_Product extends Mage_Core_Block_Template{ //Functions that would be called in phtml // they would use the $this->functionName to call them }
  44. Extensions - Controllers • Controllers are used to route requests. • Controllers should be lightweight, keep business logic out of your controllers. • No really, keep your logic in helpers or models. • Controller matches route • Class/Method is fired • Layout Object is instanced, it creates all the blocks. • You can extend core controllers: • Learn more: /app/code/community/Demo/Example/etc/config.xml <config> <frontend> <routers> <examplerouter> <args> <module>Demo_Example</module> <frontName>demoexample</frontname> </args> </ examplerouter > </routers> </frontend> </config> /app/code/community/Demo/Example/controllers/IndexController.php <?php class Demo_Example_Block_Product extends Mage_Core_Controller_Front_action { public function indexAction(){ // would fire on } }
  45. Extensions – Install / Upgrade Scripts • Uses the module version in app/etc/module/Demo_Example.xml and compares against the value in the core_resource table • Requires resource models to be setup properly • Install and upgrade scripts live in: app/code/community/Demo/Example/sql/uniquename/  Install- would contain and $installer • This will fire off on ANY request if the config_cache is cleared, “upgrading” modules, which can be confusing. • Good Write up on this: Installer scripts can be really confusing.
  46. Magento 101 – Mathew Beane – php[world] 2015 Magento Quick Start Theme and Design Guide 46
  47. Designs and Design Choices • Designs can easily become extensions and become dependent on extensions easily. • Designs are also highly dependent on the front-end HTML choices that are made. • Magento has a built in Responsive Design(RWD) • When designing for an existing or new site, there are a lot more constraints than when looking at extensions.
  48. Layout XML Files Layouts can drive how the page is displayed This example is setting the homepage to be 1 column wide: <demo_index_index> <reference name=“root”> <action method=“setTemplate”> <template>page/1column.php</template> </action> </reference> </demo_index_index> Use Commercebug to track down which pieces of Layout are being rendered.
  49. Layout XML - Templates and Blocks Layouts also drive what loads where (blocks / templates) Here is an example of displaying the demo module content block: <demo_index_index> <reference name=“content”> <block type=“core/template” name=“demo_content” template=“demo/demo_content.phtml”> </reference> </demo_index_index> Toggle on Template Path Hints to find out which piece is loading where on the page, this can also show block class.
  50. More on Templates and Blocks Templates are the key to theming in Magento • Templates are created as “.phtml” documents • Magic variable “$this” allows us to access parent block class • Template best practices:  Frontend components should be as modular as possible  Logic should only be used when coming directly from parent block class  Backend logic (i.e. querying data) should NEVER live in templates  Responsive frameworks make prototyping easier and faster  Don’t use hard URL paths, or include assets in templates. One key component to designs we have skipped over today is Handles, which are used to generate the layout that will be used to render the templates and blocks.
  51. Acumen – A great starting design Acumen is a 5 year old theme by Gravity Department • 960 Grid • HTML5 + CSS3 • High Quality • Loaded to the gills with widgets and features • Maintained by author • Not Responsive Design
  52. Magento 101 – Mathew Beane – php[world] 2015 Magento Training and Certification Learning Magento 52
  53. Magento Training StackExchange: Very active community, easy to get answers. Magento site: Tons of resources, documentations for Magento 1 is in a great spot. The on-demand videos are cheap and amazing. Blogs: Alan Storm: Belvg: Ichoo: Alan MacGregor: Alan Kent:
  54. Magento Certification Magento Certifications: • CERTIFIED SOLUTION SPECIALIST • FRONT END DEVELOPER • CERTIFIED DEVELOPER • CERTIFIED DEVELOPER PLUS “Experienced Magento professionals can validate their real-world skills by earning a Magento Certification. Magento Certification Exams are geared toward professionals who want to differentiate themselves from the competition with the ultimate Magento credential.” -
  55. Magento Certification Subjects Basics: Introduction to Magento code hierarchies, modules and configuration. Request Flow: Learn how Magento bootstraps itself and handles requests. Rendering: Understand how pages are rendered - themes, layouts, blocks and templates. Databases: Discover models, resources models and collections. EAV: Entity Attribute Value tables, explained. Adminhtml: Manage admin area forms and grids. Catalog: Find out about categories, products, layered navigation and taxes. Checkout: Covering quotes, orders and shipping and payment methods. Sales: Order creation and management. Advanced: API and Widgets etc.
  56. Magento Certification Study Guides Official study guide, a good starting point for studying for the exam. It will give you a broad overview of the subjects. On-demand course, really quite a good course even if a bit dated. Then again, so is the test. Put together by some of the community as a way to dig into examples for each of the subjects in the test. Cheap alternative, covers the entire gamut of the test and is really a great learning tool for teams. A great companion to the moderators kit, with Vinai Kopp taking you through each of the examples for the first part of the moderator kit.
  57. Magento 101 – Mathew Beane – php[world] 2015 Magento 2 It’s GO TIME! 57 Magento 2.0.0-RC1 Being Released
  58. Magento 2 – Release Information Six basic Goals of Magento 2 • Modern Tech Stack • Improved Performance and Scalability • Streamline Customizations • Simplify Integrations • Cleaner Install and Upgrades • High Quality Code & Testing
  59. Magento 2 – Under the Hood • PHP 5.7 / 7 / HHVM Support • Dependency Injection • Service Layer/Contracts • Jquery • HTML 5, CSS, LESS • Require.js • Symfony & other 3rd Party libraries • Composer • Full Test Coverage
  60. Magento 2 – Features Overview • Very modular with a strong backbone in open-source • CE will not have all of the scalability and clustering features • Many Client-side and frontend enhancements • Up to 3 master databases for separate business domains Main (Catalog),Checkout and Order • Varnish support out of the box (Swapable for FPC) • Support for RabbitMQ and other queueing systems Present in the deferred stock update feature • Asynchronous order insertion Magento 2.0.0-RC1 Being Released
  61. Magento 2 – Get Involved You can branch, make Pull Requests and they are actively participating in issues there. Central hub for all the official Magento 2 information Documentation, which is still being developed actively. You can branch and PR here as well. Magento 2 training course, on-demand. Still in development, however its very inexpensive right now.
  62. Magento 2 – Learn More Here Magento 2 Talks at php[world] • Magento 2 Dependency Injection, Interceptors, and You Joshua Warren – Today @ 4:30 PM in Ash Grove C • Magento 2: New and Innovative? David Alger – Wednesday @ 4:30pm in Potomac • Extending Magento: Fundamentals of Development in Magento 2 David Alger – Thursday @ 10:00am in Ash Grove C
  63. 63 Mathew Beane Tweeter: @aepod You – For attending. Thanks for showing up. My Family – For putting up with me making these slides. Magento Community – Very good people, deserve a lot of thanks. PHP Community – For just being so damned cool. Ben Marks - Community Magento @benmarks on twitter PHP Architect: Great conferences and real community leadership. Robofirm – They also put up with me making these slides. THANKS TO THE FOLLOWING:

Editor's Notes

  1. Magento employs the MySQL relational database management system, the PHP programming language, and elements of the Zend Framework. It applies the conventions of object-oriented programming and model-view-controller architecture. Magento also uses the entity–attribute–value model to store data. - Wikipedia You can learn a LOT about php from Magento.
  2. You cannot extend the following classes: Mage_Core_Block_Template, Mage_Core_Model_Abstract, Varien_Object, Mage_Core_Block_Abstract, Mage_Customer_Model_Address_Abstract 
  3. The same fallback strategy that is present for design layout and template files is present for skin assets. If files are not found in your selected <package>/<theme> the application will follow the fallback strategy and find the appropriate files in the default directories. We come back to this in a minute.
  4. The first place Magento will look for a file is app/design/frontend/Custom_Package/Custom_theme/ skin/frontend/Custom_Package/Custom_theme If Magento cannot find the file in Custom Package/Custom Theme it will look in: app/design/frontend/Custom_Package /default skin/frontend/Custom_Package/default If Magento cannot find the file in Custom_Package/default it will look in: app/design/frontend/base/default skin/frontend/base/default
  5. Collections implement: IteratorAggregate and Countable Model Collections as arrays that also have methods attached.
  6. Seeing 400-500 tables or more is not unusual Many are index or flat data tables.
  7. Installation: Manual and automated installation methods. And a short discussion of source control. Development: The basic parts of an extension. Designs: Layouts and templates.
  8. We are going to be moving quickly through this coursework.
  9. Installation: Manual and automated installation methods. And a short discussion of source control. Development: The basic parts of an extension. Designs: Layouts and templates.
  10. Installation: Manual and automated installation methods. And a short discussion of source control. Development: The basic parts of an extension. Designs: Layouts and templates.
  11. Keep calm, check the version # again and carry on.
  12. Installation: Manual and automated installation methods. And a short discussion of source control. Development: The basic parts of an extension. Designs: Layouts and templates.
  13. Each page request in Magento will generate several unique Handles.  You can think of the Package Layout similar to the global config. It's a large XML file that contains every possible layout configuration for a particular Magento install. 
  14. Layouts are also used to add js, css etc to the page. They are very powerful.
  15. Layouts are also used to add js, css etc to the page. They are very powerful.
  16. Service Layer is a set of interfaces used to define the public api of a module. Allows for the use of the repository pattern (abstract the way data is mapped out to an object, db can be anything)