Note: This article explains the settings for Yii 1.1. For Yii 2 there is another Wiki article.
Code completion ¶
- Exclude
yiilite.php
from index:- `File → Settings → IDE Settings → File Types`. - `yiilite.php` to `Ignore files and folders`.
- Exclude not used directories, specify resources.
- `File → Settings → Project settings → Directories`. - Mark `framework/cli/views`, `protected/runtime` and `assets` as `excluded`. - Mark website root as `resource root`.
- Specify path to your PHP.
- `File → Settings → Project settings → PHP → PHP Home`.
- If your project uses common Yii framework folder you need to include it.
File → Settings → Project settings → PHP → PHP Home → Add
.- Specify a path to
framework
directory.
- If you are writing unit tests you can include PHPUnit to get code completion:
File → Settings → Project settings → PHP → PHP Home → Add
.- Specify a path to PHPUnit.
- Complete code:
Ctrl+Space
. - Show method arguments:
Ctrl+Q
.
Enhanced code navigation ¶
In order to be able to get from render
or renderPartial
to the view, from widget
to widget class, from relations
to model classes you need to install additional plugin called YiiStorm.
Testing ¶
You should install PHPUnit to run unit tests.
- PHPUnit.
- Follow official PHPUnit installation guide.
- In your IDE:
Run → Edit configurations
. - Press "+".
Name
: anything.Test
: depending on what do you want to test select an appropriate option. Specify path.Use XML configuration file
: specifying path tophpunit.xml
. Often it'spath_to_your_webroot/protected/tests/phpunit.xml
.
- To run tests use
SHIFT+F10
.
Code completion in views
If you would like to be able to use code completion in your views you can add the comment:
/** * @var $this ThisController */
at the top, you can also add any variables assigned via the CController::render() function.
resolving "multiple declarations" when yii is in the include path
There is no "Project settings → PHP → PHP Home" now, you can add yii folder as include path in "Project settings → PHP".
It's not possible to exclude the cli/views folder when yii is "connected" as a folder in the include path.
All you can do to avoid "multiple declarations" problems with SiteController and Controller classes is to mark files [framework]/cli/views/webapp/protected/components/Controller.php and .../controllers/SiteController.php as plain text (right click on each file and select "Mark as Plain Text" in the context menu).
Notes on Configuring PhpStorm Mac OS X for Yii
In PhpStorm for Mac OS X, there are some differences:
yiilite.php
from index:PhpStorm → Preferences → [IDE Settings] File Types
.yiilite.php
toIgnore files and folders
field.PhpStorm → Preferences → [Project Settings] Directories
.framework/cli/views
,protected/runtime
andassets
asexcluded
.resource root
./Applications/MAMP/bin/php/php5.3.6/bin
PhpStorm → Preferences → [Project Settings] PHP
.PhpStorm → Preferences → [Project Settings] PHP → Add
.framework
directory.PhpStorm → Preferences → [Project Settings] PHP → Add
.Debugging settings
I think its good to have a section of "debugging" in this wiki article. I've sketched below a draft for its content. Its a big topic which probably deserves a separate wiki. Nevertheless, here it is:
Debugging on localhost
Many developers have a working web server on the same machine in which they develop. The following instructions are suitable for a simple use case where a developer run his web app on the same machine (with URL like mysite.local) and where the IDE edits the code in the doc root directly. This article is not intended to be a complete debugging PHP web app tutorial but just to hint the reader in the places to edit with basic values to use as configuring the IDE and the environment for debugging might require more work and configuration. The Internet have lots of useful tutorials these days for PHP debugging (for example (a bit old but still useful): http://www.rymland.org/en/blogs/boaz/3_mar_08/using-pdt-xdebug-debugging-drupal-projects).
Prerequisites
This assumes that you'll use the popular xDebug PHP extension for the server side debugger component (the client side is PHP Storm itself). Make sure to install it in your PHP environment where you'll do the debugging (localhost). Please refer to xDebug documentation for information about this. Don't forget to edit xdebug.ini in your PHP's configuration to have "xdebug.remote_enable=1"
Configuring web server for project
First, we're gonna need to tell the IDE about the web server it needs to talk to:
File -> Settings -> Project settings -> PHP -> Servers.
Click (+) -> Name: give a name (example: mysite localhost); Host: hostname used to access the web app from the browser (example: mysite.local is a commonly used convention); port: probably leave on 80; Debugger: xdebug
Click Apply, then Ok.
Menu bar: Run -> Edit configurations. We're gonna create a new configuration:
Click (+) -> PHP web application: Name: your preferred name (example: mysite localhost); Server: choose the server created in above steps in this drop down selector; Start URL: leave on "/"; Browser: choose your browser (you might need to configure this in advance in Settings -> IDE Settings -> Web Browsers); Other parameters - to your liking. I leave the other checkboxes unchecked.
Click Apply, then Ok.
Starting a debug session
From the tool bar, click on dropdown which now shows the name of the debug configuration you've created in previous step. Then click on the "Debug " button which is typically the second button to the right of the configurations drop down you've just selected. A debug session will be opened:
instead of adding yii in php include path add in project settings->directories
I had some issues with method showing up as not existing in current class although they were in parent class . I went to phpstorm forums and they linked back here with some additional checks to perform. I did as mentioned in this wiki and also added yii framework in settings->project settings->directories instead of adding it in external libraries and it is perfectly configured now.
Making PhpStorm know variable types even more
@codescope: also see comment http://www.yiiframework.com/wiki/92/configuring-phpstorm-ide-for-yii/#c5646 for info on using @var tag to make the IDE know the variable type. This is still highly needed or used in Yii code.
Components
How could I make php storm aware of the yii components loaded?
So that lines like this:
have auto complete for the component's attributes and methods? :)
I have seen it happen in netbeans..
Debugging
Nice guide! I finally have all the autocompletion working properly.
I attempted following boaz's guide for the debugging connection but must have gone wrong somewhere. If found a guide by the makers which requires less configuration and worked immediately (after setting the xdebug.remote_enable = 1);
Link to guide
edit: now both debugging variants work. Happy days!
Path Mappings
If there is a symbolic link in one of your libraries, you will need to tell PHPStorm about the symbolic link through a path mapping. For example, XAMPP uses the following symbolic links:
/Application/XAMPP stephen$ ls -lh total 32 drwxr-xr-x 3 admin admin 102B Feb 27 2010 XAMPP Control.app lrwxr-xr-x 1 admin admin 18B Feb 27 2010 cgi-bin -> xamppfiles/cgi-bin lrwxr-xr-x 1 admin admin 14B Feb 27 2010 etc -> xamppfiles/etc lrwxr-xr-x 1 admin admin 17B Feb 27 2010 htdocs -> xamppfiles/htdocs lrwxr-xr-x 1 admin admin 15B Feb 27 2010 logs -> xamppfiles/logs drwxr-xr-x@ 23 root admin 782B Feb 27 2010 xamppfiles
To include the Yii framework in your project,
1) Go to Preferences>Project Settings>PHP Home and add a PHP include path "/Applications/XAMPP/htdocs/yii".
2) Go to Preferences>Project Settings>PHP>Server and enable path mapping for the server. Create the following path mapping: "/Applications/XAMPP/htdocs/yii" -> "/Applications/XAMPP/xamppfiles/htdocs/yii".
Yii::app()->user...
Hey all,
If you want to have auto complete for the component's attributes and methods, Just use with that code:
In your main index.php, just include the YiiBase.php (not yii.php).
$yii=dirname(__FILE__).'/../framework/yii.php';
To:
$yii=dirname(__FILE__).'/../framework/YiiBase.php';
And just add this code after $yii include line:
class Yii extends YiiBase { /** * @static * @return CWebApplication */ public static function app() { return parent::app(); } }
and now you can use with Yii::app()->user->id and all what you need :)
Have fun :)
Autoadd @var
Hello, for a bigger project I needed the @var in all my models from database so I wrote a yii command to do this automatically. I've posted it here. Please try it carefully it worked with my codebase quite well but could go rampage when the code is strangely formatted/structured.
PhpStorm 5 is out with "Yii integration"
I'm no expert so does anyone know how to take advantage of it (besides what's described here)? Thanks in advance.
Letting the IDE know about behavior-provided methods?
Hi,
Is there a way to let the IDE know automatically the methods provided by behaviors attached to a model, as defined in its 'behaviors()' method?
I emphasize 'automatically' as I know I can use a doc block above the model class, with a comment such as:
/** * @method generateUniqueSlug() */
I want to avoid needing to manually write (and maintain) all those methods.
If there's no such solution, does anyone know about an open bug/feature-request on this?
Thanks
Re: Luke Jurgs at 2011/10/28 09:23am ^^
Hi Luke,
My site/index.php file has this at the top.
/* @var $this SiteController */
But I cannot get intellisense on a variable passed from the actionIndex like so.
START VIEW
<?php echo $num; ?>
END VIEW
START CONTROLLER CODE
public function actionIndex() { $num = 23; // renders the view file 'protected/views/site/index.php' // using the default layout 'protected/views/layouts/main.php' $this->render('index', array('num' => $num)); }
END CONTROLLER CODE
Is this what you are talking about working? Any ideas? I have followed the other steps in this post.
Re: johnsnails at 2013/06/28 07:29am
Hi John,
PHPStorm uses any @var phpdoc declaration as a type hint. It doesn't understand how the view is actually being rendered, since it only really has a lexical/syntactic parser and whatever magic they bake into "intellisense", it isn't a fully fledged interpreter.
So in your case you would have to hint at what $num is. E.g:
/** @var $num int */
Codelobster
I suggest to try Codelobster PHP Edition
This IDE has special plug-in for Yii autocomplete and so on.
Yiistorm
Yiistorm 0.9b create phpdoc inside new "View" after $this->render('view') autocomplete
Yii storm
v0.9.4b - 29.09.13
several fixes with autocompleter
Re: Re: johnsnails at 2013/06/28 07:29am
Hi Luke,
I feel terrible that I only just now noticed you wrote back to me.
Much appreciated, thank you for clarifying that.
Cheers!
gii-modeldoc-generator
I released a gii generator that will generate phpDoc code for all your models - gii-modeldoc-generator
It really helps with phpStorm's autocomplete.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.