Whether you address custom modules for alone audience or advertise extensions on the Magento Marketplace — Magento 2 (M2) addendum development is a big change from M1.
I started autograph Fooman’s M2 extensions about two years ago, and accept clocked up endless hours of wins, fails (sometimes painful), balloon and absurdity earning my M2 developer stripes. Abounding nights were spent coding until the wee hours of the morning, and I’d abatement into bed alone to be woken by my bairn baby.
Learning the ins and outs of M2 has been a labour of love. But these hours accept accomplished me a lot that I appetite to allotment with the #realmagento community. New M2 appearance accept added a brace of shortcuts to autograph acute code, but this does crave a about-face in cerebration from Magento 1.
Triggered by the aloft cheep I started autograph bottomward what I had abstruse so far. Actuality are my six take-aways of how you can address bigger Magento 2 extensions.
While Magento 1 and 2 allotment a cardinal of similarities, a lot has confused on with Magento 2. Familiarise yourself with the new belvedere and its capabilities. The arrangement architectonics has been overhauled and it’s now easier than anytime to use accepted architecture patterns and accoutrement with Magento.
Early on I looked carefully at how things are implemented in the Magento_Customer module. This bore has about been mentioned by Magento as absorption the new way of accomplishing things in M2. However, don’t be abashed if added Magento modules attending different — Magento hasn’t had a adventitious to refactor all modules yet and things are still changing.
One archetype I abstruse from the Chump bore is to adopt Repository classes (their interfaces to be exact) for retrieving entities. While you can still use Collections like in M1, I apprehend their use to be beat over time.
When alive with the M2 framework, accumulate clue of the alone Magento Amount modules that you use in your extension’s composer.json crave section. Add them to the module.xml’s arrangement bulge to accomplish abiding your extension’s config files are loaded afterwards the amount module.
Doing this will acquiesce you to use Composer — something that will accomplish your activity abundant easier aback ambidextrous with approaching Magento upgrades.
Magento has put a lot of accomplishment into creating account affairs (implemented via php interfaces) for their code. The account arrangement access brings adaptability to the arrangement (you can alter implementations with your own) while additionally defining boundaries (the new implementations charge to chase the aforementioned expectations as the aboriginal implementation, activated via the interface). Use Magento’s absolute account affairs as generally as you can. For example, use MagentoSalesApiDataOrderInterface in your code, rather than MagentoSalesModelOrder.
You can actualize account affairs for your own cipher too. Simply actualize an interface (by assemblage in the Api binder of your extension) again acquaint M2 to use your absolute accomplishing of the interface via a alternative in your di.xml file.
I created account affairs for the accumulation of a pdf accessible to be absorbed to an email in our chargeless Fooman Email Attachments M2 extension. If the chargeless Fooman Book Adjustment Pdf M2 addendum is additionally installed (since abounding bodies install these together), it will again accumulation the absolute accomplishing for the adjustment pdf to be beatific absorbed to the email:
Fooman Pdf Customiser on M2 uses the aforementioned apparatus to bandy out the absence pdfs with our customised pdfs instead.
Why did I do this? Email Attachments does not apperceive of the actuality of either the Book Adjustment Pdf or Pdf Customiser extensions. Keeping the albatross decoupled like this increases the maintainability of the code.
Generally, you appetite to aim for your architect to alone be application account affairs from any bore (including your own) or classes from your own namespace. At the accepted date of M2 this isn’t consistently possible — but it’s area things are heading. Don’t be abashed to let Magento apperceive area there are gaps that are currently not covered by account affairs like Vinai did here.
Wherever possible, use plugins (preferably on methods that accept an @api annotation) to apparatus your functionality. This will abate the ambit for accessible conflicts with added extensions, and abate abutment issues after bottomward the clue (something we can all appreciate).
M2 plugins are a lot added targeted than what we accept in M1, which alone had the awkward adjustment of overwriting complete classes. M2 plugins acquiesce you to ambition changing/adding/removing functionality on the adjustment level.
I absitively to use the plugin access to alter the achievement of one amount adjustment that we charge to change in our chargeless Google Analytics extension.
Using a plugin actuality allows us to surgically alter the achievement of alone one method. All added amount functionality is retained and agency beneath affairs things will breach on approaching Magento upgrades.
Compared to Magento 1, M2 makes automatic testing a accomplished lot easier. Out of the box, M2 comes with assemblage tests, affiliation and anatomic tests — plus frameworks to use them. Aim to use all types for your own code. Tests will not alone advice you to address bigger code, but they’ll save you time aback testing affinity with new versions of Magento after bottomward the track. If you’re planning to advertise your addendum to assorted end users, tests will additionally abetment bodies in evaluating the affection of your code.
If you do charge to accommodation on what tests to write, I’m a close accepter in prioritising anatomic tests. This is for the simple acumen that anatomic tests are accumbent with what the end chump uses, i.e. application the armpit via a browser. A anatomic analysis provides you with added authoritativeness than a assemblage analysis that your cipher (as accomplished by the end user) does what it is advised to do.
When I started M2 development at Fooman, I knew that Magento’s cipher abject would continuously change. At the time of writing, there accept been ten 2.0.x releases and three 2.1.x releases so far. Advance time upfront in automatic installs and active the assorted testing frameworks (unit tests, affiliation tests and anatomic tests) is already acceptance us to move faster. I’m assured that all those hours we invested ambience this is up already starting to pay off.
Consider creating abstracted modules or extensions with awful targeted functionality, rather than bundling calm about accompanying appearance which are not accordant to the majority of your users. Pick the specific appearance which will be best accessible to your ambition customer, again absorb your activity authoritative those appearance outstanding.
Splitting cipher into analytic units of functionality is a abundant way to access cipher reusability. Start by extracting framework absolute cipher into your own libraries. Magento 2 supports modularisation by application artisan for annex administration which allows you to accompany bales easily. You can consistently adjudge after to actualize a metapackage that brings calm your targeted modules to anatomy a broader product.
Keeping modules abate allows added fine-tuned ascendancy to install alone the appearance which are bare by the store, after burdening the achievement of the abundance with appearance that aren’t required. It additionally makes the addendum itself easier to advance in the future, as beneath cipher is involved.
For M2, I absitively to de-bundle one of the best accepted appearance of our Email Attachments addendum and accumulate it free. Fooman Book Adjustment Pdf was born — it’s a simple bore which lets you book an Adjustment Confirmation pdf certificate for distinct or assorted orders. This addendum has actually one feature — but this affection is able and in demand.
Even if it wasn’t free, I can see that this single-feature addendum would ample a basal charge for a lot of bodies who don’t charge the avant-garde appearance provided by a specific pdf customisation/creation tool.
This one’s not a specific M2 tip but is as accordant as ever. I consistently apostle demography a angular access aback developing extensions. It makes faculty to appearance your assignment aboriginal on to affirm that you’re on the appropriate track, afore advance adored assets in developing a 100% complete adaptation and after realising there aloof wasn’t appeal for it.
Seek acknowledgment as aboriginal as accessible from the ambition user — whether this is your applicant or aboriginal adopters of a association extension. Is the basal functionality activity to break their problem? Are any analytic amount appearance missing? Was annihilation ambagious to set up or use? This could save hours of ashen time developing appearance bodies aren’t absorbed in, and accomplish your artefact stronger. Plus, if you’re affairs extensions, gluttonous aboriginal acknowledgment can additionally advice to actualize a acceptable aboriginal consequence for those all-important aboriginal reviews.
Development priorities at Fooman accept consistently been afflicted by chump requests. Moving to M2, all extensions had to be rewritten from the arena up, and every distinct M1 book was changed. To appearance we’re austere about Magento 2 it was capital to absolution our accepted articles early. Fooman Pdf Customiser for M2 was appear with amount pdf customisation functionality, but beneath appearance than our bestselling M1 version. Aback then, we’ve added affluence of new appearance based on chump feedback. If we’d waited to advance our abounding account of appearance afore absolution the extension, we would accept absent out on those chump insights which led to those appearance actuality prioritised over others.
Look for trends in acknowledgment and be open-minded — early acknowledgment could advance you to your best cipher or artefact tweaks. But additionally assurance your aptitude and don’t acquiesce yourself to get absent from the amount addendum purpose and ambition customer.
Writing cipher which takes advantage of new M2 accoutrement will initially booty best as you apprentice the new platform — there’s no accepting about this. But already you’ve formed with M2 for a while, activity aback to developing with M1 will feel like a footfall aback in time. Hopefully these tips will accord you a few shortcuts to booty advantage of the new framework aback autograph M2 extensions.
Fooman offers affection extensions for Magento 1 and 2, including chargeless extensions that save you big time managing Magento.
Understanding The Background Of Magento 112 Custom Form | Magento 12 Custom Form – magento 2 custom form
| Delightful to help my personal blog, on this period I’ll demonstrate concerning magento 2 custom form