5 Dec, 2017

Developing in Magento 2

Working at Indaba gives me the opportunity to work on multiple projects, all with different requirements, customizations, use cases, database sizes and traffic.

Now that I have a few Magento 2 projects under my belt, I think it’s time to share the tools and shortcuts I use to help any developers new to Magento.

Local Environment

First, let me preface this by describing my working environment. I do all my development on a 13″ Macbook Pro w/Retina display. The system has a 2.7Ghz Intel Core i5 and 8GBs of ram. I mostly work in the LAMP stack so I use MAMP Pro to handle the environment stack. MAMP makes adding and editing hosts extremely easy so I can focus on development and not my local environments. To manipulate databases graphically, I use Sequel Pro. As a more visual person, I find this helps me to better understand the data. For importing, exporting, and other tasks I use the MySQL CLI because I find this to be more resilient for the large databases I tend to work with.

Code Editor and IDE

Magento is PHP-based and because of that I use PHPStorm as my IDE. This in conjunction with a plugin called Magicento simplify and save time developing. PHPStorm has a lot of useful features. The ability to go to a class by simply clicking a function cuts down on me having to search through directories to find the file I’m looking for. PHPStorm’s integrated terminal opens to your project directory and let’s you separate your terminal windows by keeping them in the same window as your code editor. PHPStorm also has xdebug built-in allowing you to easily place stopping points and step through your code as it’s running which makes debugging a piece of cake. Lastly, I love PHPStorm’s Git integration. While I do mostly use the CLI version of git for setting up repositories, committing, pushing and pulling, I use PHPStorm’s git integration for file comparisons, viewing file history and finding out which code was written by which developers. The last feature is especially helpful since most projects I work on are collaborative with other developers. I won’t go into smaller things like it’s built in linters, remote servers, and other common IDE features but if you’d like to learn more be sure to research those yourself.

PHPStorm has some pretty powerful built-in features but, if you’re a Magento developer, you’ll want to invest in a plugin called Magicento. Versions exist for both Magento 1 and 2 and I would consider both to be crucial. With Magicento PHPStorm becomes extremely aware of the Magento platform as a whole. Adding magento specific autocomplete, understanding different correlations between module config files, and even automating common tasks. The most useful feature is the ability to create an extension framework, something that would normally take a half hour or more in a minute by simply checking some boxes and clicking create.

Magento CLI

Magento 2 introduces a robust CLI. It comes with many commands you’ll become reliant upon over your time developing. All Magento CLI commands must be run from your project/magento root directory. The most important to me are below:

php bin/magento setup:upgrade
This is the command you will likely run the most. Installing plugins, changing some settings, modifying classes and making frontend changes will all require running this after you save your changes.

php bin/magento cache:flush

When you change JS files, CSS files, .phtml templates, etc you will have to run this command (assuming caching is enabled – which it most likely will be).

php bin/magento deploy:mode:set developer

When developing it’s important to put Magento 2 into developer mode. This tells Magento to create symbolic links to all media and code that hasn’t been generated and cached which helps speed the development process but also forces magento to fetch those assets on each page load. Because of this, page response time suffers.

php bin/magento setup:static-content:deploy

After you clear cache, run setup:upgrade, or put magento into developer mode, you’ll find that magento loads extremely slow. The command above tells Magento 2 to generate these files all at once keeping your last changes but not showing new changes.

php bin/magento setup:di:compile

When Magento 2 is in Developer mode you’re not supposed to have to run this command though I find that when creating modules or new PHP classes I often do. This command tells Magento 2 to update all system and module related cached classes.

It can be cumbersome to have to run all of those commands so I’ve created some bash aliases which I find cut down on time I spend typing common commands.

alias phpu="php -d memory_limit=-1" // Run PHP without Memory Limits
alias magento="php bin/magento" // Magento 2's CLI
alias upgrade="magento setup:upgrade" // Less Characters for setup:upgrade
alias css="rm -rf pub/static/frontend/[themename]/Base/en_US/css/* var/view_preprocessed/*" // Force Magento to regenerate CSS files only (replace [themename] with your theme name
alias front="upgrade && rm -rf var/view_preprocessed/* pub/static/* var/cache/* var/page_cache/* var/generation/* && magento cache:flush" // Runs all commands to get frontend changes to show
alias static="phpu bin/magento setup:static-content:deploy" // Deploy static content without PHP memory limits (useful for multisites)
alias statict="phpu bin/magento setup:static-content:deploy -t" // Deploy static content to a specific theme (after -t enter your theme name [Vendor/theme]
alias compile="phpu bin/magento setup:di:compile" // Compile all PHP classes
alias dev="magento deploy:mode:set developer" // Put Magento into developer mode
alias branch="composer install && composer update && dev && front && static" // Runs all required commands when switching project branches
alias brancht="composer install && composer update && dev && front && static -t" // Runs all required commands when switching project branches but only deploy's static content for specific theme

To add these commands to your bash profile you’ll need to edit ~/.bash_profile. After saving your changes, you’ll need to run source ~/.bash_profile for those changes to take effect.

Opinion

All of the above tools and shortcuts make working with Magento 2 much less cumbersome but the platform itself can still be a pain to work with. While the CLI is a great addition, commands like setup:di:compile and setup:static-content:deploy can take minutes to run adding hours to project times. The biggest gripe I have with Magento 2 development revolves around the indexer. Indexing has always been slow in Magento but unfortunately there are still many bugs and inefficient code causing large databases to take hours and sometimes days. I think a progress bar should be implemented because you never know if your index failed or if it’s just going to take a while.

Magento 2 is definitely a platform improvement. The changes to CLI, built in composer and symphony all make magento feel more like a platform and less of a collection of files. The added CMS features like scheduled changes and promotions, easier product editing, and an improved marketplace. Unfortunately tho, I feel the platform can at times feel rushed. The added development and debugging time spent can make the cost of implementing an M2 site much more expensive than Magento 1 or other platforms like Craft Commerce. I’m not sure the new features outweigh the added development costs.

You may also like...