Navigate in Twig namespaces in PhpStorm

By Camilla Krag Jensen naxoc |

I've gotten so used to have everything be clickable or navigable in PhpStorm. "I wonder what that function deep in the vendor folder looks like?" - I command click on Mac or ctrl click on Linux (or command+b/ctrl+b), and boom I go to the function definition. I don't know how I lived without it in the past.

But when I was writing a lot of Twig this spring I was sad that the file in Twig was not navigable like this: Add Twig namespace in PhpStorm

It took me forever to find out how to set it up. You need to install the Symfony plugin and either manually add the namespace under "Languages & Frameworks" -> "PHP" -> "Symfony" -> "Twig/Template": Add Twig namespace in PhpStorm - or even better, you can add a file called ide-twig.json where you tell the editor where to find files from a namespace. If you namespace was called "mynamespace" your file could look like this:

  "namespaces": [
      "namespace": "mynamespace",
      "path": "templates/stuff"

where the path is relative to your Twig templates.

The feature with the ide-twig.json file was added in the Symfony Plugin in version 0.11.110 years ago, but I've not come across much documentation for it. So now it's in this blog post.