Odds are that you are using PHPStorm as your favorite IDE if you are working with PHP Indeed PHPStorm is a powerful tool which can assist a developer in so many unbelievable ways. I have to admit that I don’t know many of those awesome features which can make your development life easier. But I learned some nice tricks when working with Magento 2 which I want to present in this blog post.
PHPStorm uses live templates which are ready to use templates. Many times you deal with recurring code or so called boilerplate code. In Magento 2 you also have to deal with the same code patterns from file to file. But there is a solution for that: Christian Münch of Netz98 wrote some live templates for Magento 2. You can download them from this gist.
In order to install them follow these instructions:
- Copy the raw XML
- Go to PHPStorm -> Preferences -> Live Templates
- On the right you can click the little + in order to create a new template group. Name it Magento 2 or whatever you like
- Paste the copied XML into it. PHPStorm will magically transform the pasted XML into templates which can be previewed.
Once you installed the live templates you can use them like described in the following (example):
- Create a etc/config.xml file and place the cursor into it
- Hit CMD + J in order to pop up the live template suggestion box
- Type magento2:xml:config into it and select the suggested templates
- Hit enter in order to see the template! No need to copy or write that code again! No copy errors anymore
Setting up schema validation for XML files
One of the many changes in Magento 2 is the introduction of XML schema validation. Those schema files are included in the Magento root, so it depends on where the Magento root is on your local development machine. If you use relative paths like the following you always have a fixed location of your Magento root:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="Vendor_Module" setup_version="1.0.0" /> </config>
But there is a much easier solution for that: URN highlighting. Follow the instructions of the documentation in order to setup the URN highlighting. It brings you two main advantages:
- Schema validation
- Autocompletion while typing (We all had that one time searching for the error in an extension for hours just to find out it was a typo in the configuration XML )
So far so good for a start. This blog post can and will be updated in the future with more tricks and settings. If you have some cool PHP Storm features for Magento 2, feel free to tell us in the comments section and I will add them to this post! Also feel free to share the post.