Back to blog home

Magicento – a Magento developer’s magic toolbox for PhpStorm

When working with a complex system like Magento, the need for proper development tools is vital for speed and precision. One of those tools is PhpStorm, the “most intelligent PHP IDE” as it states on their website. Features like code quality analysis, integrated version control, and built in testing and debugging tools make development not only easier, but more enjoyable.

So what is Magicento?

Magicento is a PhpStorm plugin for Magento developers. It’s full of advanced development

features like Goto to destination class from any factory method, and jump to any PHTML template from layout XML files. It also offers autocompletion for factories, XML files and class names, and a magic context aware menu for so many other great tools. More on this later on.

Installation

For the purpose of the tutorial I will assume you already have PhpStrom and Vagrant installed and setup.

Head over to Magicento and get your copy of Magicento. There is a 15 day free trial version, but its features are limited. To truly appreciate the magic of Magicento I encourage you to splash out the $29 for the full version. It is well worth it.

After your purchase you can download your version using the link provided. Head over to PhpStorm and go to preferences (cmd + ,) and choose Plugins from the menu on the left.

Click on “Install plugin from disk” and locate the .jar file you downloaded.

Once installed you will need to restart PhpStorm for the changes to take effect.

Setup

OK, so now you have Magicento installed and you are pretty much ready to go. Well almost. Even though you can immediately use some of the features, there are a few extra steps required to get you going with a Vagrant virtual machine environment.

First thing’s first, head back into your PhpStorm preferences (cmd + ,) and at the bottom of the menu on the left you will see under “Other Settings” the new “Magicento” menu.

These settings are on a per project basis so any changes you make here will need to be replicated on each project. If like me, you spend time at the start of the project to setup PhpStorm, then you will find it very easy to add this to your current workflow.

Starting from the top you have basic on off switches for some features as well as some

configuration paths: –

  • Enable/Disable Magicento
  • Automatic Autocomplete
  • Advanced ForEach inference
  • Disable Magicento Automatically after IDE crashes
  • Absolute path to app/mage.php
  • Paths to Magento code
  • Enable/Disable PHP Execution
  • Evaluate in Magento
  • Current Magento Store ID
  • Layout Features
  • Automatic inspect of $this
  • Automatic check for rewrites
  • Rewrite conflicts solved
  • Automatic check for malformed config.xml

Most of them will be left default or blank, however. When working with your VM you need configure this with specific settings.

Check the Enable/Disable Magicento setting –

Leave the next three settings as default –

As stated in the settings, the “Advanced ForEach inference” is a development feature and not yet available. Automatic Autocomplete is a core feature but if you find your IDE crashes often then you should consider disabling this feature.

The last option there “Disable Magicento Automatically after IDE crashes” is a nice feature that in the event your IDE does crash, Magicento will disable itself so you don’t get caught in a freezing loop on restart. That being said, I have not had any issues with this so far.

The next setting, “Absolute path to app/mage.php” may need a little tweak, depending on how you have your project set up. By default, Magicento will assume your Magento root folder is the project root folder and will autofill this field for you. However, if like me you use a different project structure and you have your Magento root in a subfolder like “public” or “magento”, then you should update this field to match. This is an important step as Magicento needs to know where the mage.php file is to execute some tasks.

Leave “Paths to Magento code” empty – this will auto update after you apply your settings.

An important step here is the “Enable/Disable PHP Execution” settings. Once selected you will see quite a few new options –

We absolutely need the PHP execution for some of the core features like template hints and cache flushing but we can’t use the PHP interpreter as this is on the host machine so we need to select “Use HTTP”.

Add the URL of your Magento installation on your VM. My URL is magento.dev/.

Note the large warning –

This will create a file called eval.php accessible via HTTP, make sure you are not adding it to VCS or deploying it to production.

Remember to add this to your .gitignore file.

Since we are using a VM, the source code is in a shared location on the host machine and there is no need to check “Upload eval.php automatically using the deployment server config”.

Magicento has a small delay in the time taken to upload the file and will try to execute the file before it’s available on the server, which will result in a failed execution. For this reason we simply leave it unchecked.

Since we are executing the PHP over HTTP we need to ensure the file is accessible over the web.

We do this my selecting the “Use var folder”. However, there are a few different methods of getting this to work since by default this is a restricted directory.

You will see the option for username and password using basic authentication. This is useful if you want to lock down the folder with a .htpasswd file. Since we are working in a locally accessible development environment with no access to the outside, it might be better to just remove the restrictions on the var folder.

If you are using an Apache based web server you can edit the .htaccess file located in the

“/magento_root/var” folder. Remember and do not commit this file back into the repository.

Change –

Order deny,allow
Deny from all

to –

Order allow,deny
Allow from all

If you use NGINX as your web server then you should edit your server block to remove the restrictions – that’s if you have already set this up correctly to restrict this directory. You should have something similar to the following setup as a standard Magento server block.

location ^~ /app/ { deny all; }
location ^~ /downloader/ { deny all; }
location ^~ /includes/ { deny all; }
location ^~ /lib/ { deny all; }
location ^~ /media/downloadable/ { deny all; }
location ^~ /pkginfo/ { deny all; }
location ^~ /report/config.xml { deny all; }
location ^~ /var/ { deny all; }

Simply comment out the last line to remove the restrictions, this can be achieved by adding a “#” at the beginning of the line.

#location ^~ /var/ { deny all; }

Please note that removing restrictions on these folder is not a recommended solution if you are working on a remote host other than your VM. This is not a solution for a shared development or staging server.

Magicento is now set up. Lets use some of its core features.

Features

Goto

Usually when you try to use PhpStorm’s Goto feature (cmd + click, cmd + B or middle click) on something like a factory method for some helper you would get the following –

But with Magicento, you are taken to the helper class immediately. Unless of course you have

rewritten the class, or you are using some symlinking tool like Modman or Composer to include Magento, then you would see the following:

You can also jump to a block class from the matching XML definition –

And of course, you can go to the corresponding PHTML template from any layout xml file. Jumping to PHTML files will show all files available for that definition i.e. all packages and themes.

One of the best things about this feature is if you click on any Mage::dispatchEvent call magicento will show you all observer definitions for the corresponding event.

The Magicento menu

This is a context aware menu – that is, the menu options change depending on where the cursor is and which file you are viewing. For example:

As you can see, the real power of Magicento is in the context menu. To see this menu in any file press (Option + M).

The PHTML file menu allows you to jump to the corresponding block, jump to the xml layout

definition, add @var $this comment definition, copy the template, compare template files, and jump to the template from another package.

The XML file menu allows you to copy the layout file, compare layout files, and both the PHTML and XML menus allow you to copy the relative path.

The PHP block menu will allow you to go to the template file for the block, go to the block definition in the layout xml and you can rewrite the class into your own module. This will create the file and folder structure setup, the rewrite in your module config.xml, and create the file in the correct location of your module with names correctly defined. The rewrite class feature is not just for PHP block files, you can rewrite any php class file using this feature.

Create a module

Open the Magicento menu (Option + M) and click on “Create Module”. You will now be presented with a popup for you to define the module details –

Choose your code pool, namespace, module name and define a version. If your module should depend on another module being loaded first you, should choose that module or multiple modules.

The next part will allow you to choose which module elements you need. Perhaps you only need a block or maybe you need installer scripts. You can choose multiple here.

Once you click OK, Magicento will create all the files required in the correct place as well as open the new module’s config.xml file.

Create a model triad

A model triad consists of three things, a model, a resource model and a resource collection. You can quickly create this structure for your module by choosing “Create Model Triad” from the Magicento menu. At this point you will be presented with the following popup:

Choose a model name, table name and your resource. The alias and primary key is automatically filled. This will create the structure you require –

 

Of course you will still need to write your installer scripts to create your custom database table(s).

Create a Controller

Creating a model controller for both frontend and adminhtml could not be easier. This menu option will give you the following popup –

If you are in a module file when you choose to create a controller, Magicento will fill in your codepool and module definition for you. Choose if the controller is for the frontend or adminhtml, choose a frontname, a route name, which router to use, set controller URL, and choose the path if you wish to use different folder structures.

Once created, Magicento will update your config.xml will the details you defined for your route leaving you free to code your controller with the desired functionality.

Create Grid & Form

One of the latest features of Magicento is the ability to quickly create an adminhtml module with grid and form. Like the “Model Triad” there are quite a few files required for an adminhtml grid and form to work. Thankfully Magicento can create these effortlessly for you.

This popup is slightly bigger than the others but to get started you can leave most of the default settings selected.

Add your Model class name; here it is pre-filled with “NAMESPACE_MODULE_Model”. Make sure you add a model that already exists as Magicento does not create the models and only references them for your collections. Add a model title of your choosing and move onto layout.

When dealing with the layout you can instruct Magicento to use a layout xml file and define what that file should be. Please note that the file needs to exists for this to work. If you don’t have a layout file, you can choose to create blocks inside the controllers. This is not an ideal solution as you want to separate layouts away from controllers. However, for the purpose of this example I am leaving it as “inside controllers”

The next section is all about grids and forms. You can choose to deselect one or both of these, although deselecting both would be pointless.

Add “Grid Container” block next. It’s worth keeping all your backend blocks grouped by space i.e Adminhtml so I have chosen a name of Adminhtml_Grid. Magicento will create this folder structure in the correct place and create our block files.

Features Over HTTP

The Magicento menu also has a few great features, which are executed by calling that eval.php file we configured during setup. One of my most used Magicento features is the “Flush Cache”, which does mimics the “Flush Cache Storage” button within the Magento admin.

Another favourite feature is “Toggle Template Hints”, which, as you would expect, toggles the path hints on and off. You still need to configure if you want block paths added to the hints in the Magento admin. The great thing about executing the path hints using Magicento is it will also turn them on for the adminhtml section – something the default configuration does not do.

Conclusion

With every new and productive tool we add to our workflow there is always a learning curve of some kind, or the discipline to use those tools when we need them. The important thing to

remember is that there is no one tool that caters to the needs of everybody and you should use the tools that fits in your workflow effectively.

Setting up your project to work with Magicento can be a little repetitive and daunting at first, but once you get in the habit it becomes second nature. Without Magicento my development workflow would take me twice as long for the cumbersome tasks like setting up modules and looking up model definitions. Just using the flush cache feature improves the feedback loop I have when making simple changes to the front end.

 


Article update
This article was originally published under Session Digital, which unified with Inviqa in June 2016. For more information about the unification visit https://inviqa.com/new-era.