38

I'm using PHPStorm 8.1 as IDE in a php project (laravel 4.1 project with twig on view side)

Will be great to have an autocomplete hint on vars on "twig side" like you can see here on this phpstorm plugin for symfony2:
http://symfony2-plugin.espend.de/languages/twig/index.html#phptypes

Basically in my twig view I declare the var "type" using a comment, then I would see hints on my model (farmaco):

   {# farmaco \Farmaco #}
   {% farmaco.  %} <-- I would see hints -->

Note: I'm not using Symfony, however I've the symfony plugin installed but the autocomplete doesn't work for me on twig files for my laravel project.

3
  • Do the twig templates have a html or twig extension?
    – Potherca
    Jun 22, 2015 at 11:08
  • both: template.html.twig, however the problem maybe the namspace, in L4 it's not explicit inside classes but handled by composer / autoload classmap .. Jun 22, 2015 at 14:51
  • 1
    I am running into similar issues. I am looking to see if I can get things working for a project that does not use Symfony...
    – Potherca
    Jun 23, 2015 at 8:07

1 Answer 1

64

You seem to be missing the @var annotation. The correct form would be:

{# @var foo \FooService #}
{{ foo. }} <!-- press CTRL+SPACE for typehints -->

I got this working for a non-symfony project without issues. Be sure to check that the symfony plugin is actually enabled for the project (it isn't by default) and restart the IDE after you enable it.

Also make sure that the type-hint point to the fully qualified class name.


As a sidenote, I have not (yet) gotten this to work for properties, i.e. foo.bar.* when the property is accessed through magic (using __get). For those instances the property needs to be put into a variable of its own:

{# @var foo \FooService #}
{{ foo.bar. }} <!-- no typehints for bar :-( -->
{% set bar = foo.bar %}
{# @var bar \BarService #}
{{ bar.  }} <!-- press CTRL+SPACE for typehints :-) -->
3
  • 2
    thanks, finally var and symfony plugin make autocompletion working. About properties they works in my case because I use in my models generated annotations i.e.: @property string $permalink they are built automagically by github.com/barryvdh/laravel-ide-helper Aug 5, 2015 at 14:04
  • 1
    Works for Silex 2 as well. Thanks!
    – xarlymg89
    Feb 7, 2018 at 14:44
  • 3
    Works with method references in deep-assoc as well now, for whomever this may concern ;)
    – Klesun
    Sep 8, 2020 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.