Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Magento 2 module development

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →

Introduction to Magento 2 module development

This presentation was given at the PHP Antwerp meetup on 28/06/2017

Avatar for Joke Puts

Joke Puts

June 28, 2017

Other Decks in Programming

Transcript

  1. What can I do with it? §Create a composer package

    §Create customizations for your commerce platform §Sell them on the Magento Marketplace
  2. Adding a customization to your installation? Start in the app/code

    directory Creating a reusable feature? Create a composer package
  3. Model-View-Control pattern § Block § Controller § Model § Setup

    § etc § frontend § routes.xml § adminhtml § routes.xml § view § frontend § adminhtml
  4. Dependency injection §DI via constructor §Api: interfaces for a module

    §etc/di.xml, etc/frontend/di.xml, etc/adminhtml/di.xml §Class preferences §Constructor arguments
  5. DI: Constructor arguments <type name="Magento\Customer\Model\Visitor"> <arguments> <argument name="ignoredUserAgents" xsi:type="array"> <item

    name="google1" xsi:type="string">Googlebot/1.0 ([email protected] http://googlebot.com/)</item> <item name="google2" xsi:type="string">Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)</item> <item name="google3" xsi:type="string">Googlebot/2.1 (+http://www.googlebot.com/bot.html)</item> </argument> </arguments> </type>
  6. Plugins • Modifies behavior of a public method of a

    class • Before method • Runs before the method • Receives the arguments of the method • After method • Runs after completion of the method • Receives the output of the method • Around method • Runs both before and after the method • Receives the arguments of the method • Receives a callable, which will give the output of the method
  7. AttributeValidation.php public function aroundValidate(AbstractBackend $subject, Closure $proceed, DataObject $entity) {

    $isAllowedType = !empty(array_filter(array_map(function ($allowedEntity) use ($entity) { return $entity instanceof $allowedEntity; }, $this->allowedEntityTypes))); ... return $proceed($entity); }