Professional Documents
Culture Documents
Tweet
Laravel is a free, open source PHP web application framework. It is built on top of several Symfony components,
and provides a development framework that makes common tasks such as authentication, routing, sessions and
caching much easier to implement.
In this tutorial, we'll see how we can develop Laravel applications using PhpStorm taking advantage of the Laravel
plugin for PhpStorm and the Laravel IDE helper. Make sure to explore the generic PhpStorm tutorials and Laracast'
s PhpStorm tutorials to learn more about Laravel and PHP development using PhpStorm.
Prerequisites (plugin installation and configuration)
1. Ensure Composer is initialized
2. Install the Laravel IDE Helper
3. Generate the PHPDoc Helper File using Artisan
4. Install and enable the Laravel Plugin
Laravel Framework Support in PhpStorm
Code Completion and Navigation for Controllers and Routes
Code Completion and Navigation for Views
Code Completion and Navigation for Configuration and Services
Code Completion and Navigation for Translations
Automatic PSR-4 Namespacing
Blade Template Support in PhpStorm
Code Completion for Braces and Directives
Sections Support
Code Completion and Navigation for Extends and Includes
Generating Code with Live Templates
Command Line Tool Support for Artisan and Composer
Debugging Laravel Applications with PhpStorm
Laravel Unit Testing with PhpStorm
Database Support in PhpStorm
Once installed, we have to add the Laravel IDE helper as a ServiceProvider into our application. In the app/config/app.php file, add 'Barryvdh\
LaravelIdeHelper\IdeHelperServiceProvider' under the providers element:
<?php
return array(
// ...
'providers' => array(
// ...
'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider', // Laravel IDE helper
),
// ...
);
Laracasts also has a video tutorial available on enabling PhpStorm support for Laravel Facades using the Laravel IDE Helper.
The easiest way to do this is by enabling command line tool support for artisan. From the settings, add a new command line tool under Tools |
Command Line Tool Support. The tool type is a Tool based on Symfony Console. Next, provide the path to artisan:
Once saved, we can use artisan from within the IDE. The Tools | Run Command... menu (Ctrl+Shift+X or CMD+Shift+X on Mac OS X) provides
completion for all artisan commands that are available. Run the artisan ide-helper:generate command to generate the required PHPDoc
information.
The Laravel IDE Helper may have to be run after changing or adding services, controllers, models and views. The Laravel IDE Helper
GitHub page provides additional tips on running it, for example, after performing an install or update of Composer dependencies.
Another options is using File Watchers in PhpStorm to automatically regenerate this file when, for example, composer.json is updated.
Restart the IDE to complete the installation of the plugins. Next, we will have to enable the Laravel Plugin in our project. We can do this from Setti
ngs (Preferences) | Other Settings | Laravel Plugin | Enable Plugin for this Project. We'll have to restart the IDE once more to load the
plugin's additional features for Laravel.
In case of any problems with the completion and navigation support provided by this plugins, select File | Invalidate Caches / Restart to
reindex your project. Running artisan clear-compiled and artisan ide-helper:generate may also be required.
We can also navigate to the controller using Ctrl+Click (CMD+Click on Mac OS X) or Go To Declaration (Ctrl+B / CMD+B). Simply hovering the
mouse with the Ctrl or CMD key pressed will show additional details.
Just like with controllers, we can navigate to our views as well. Using Ctrl+Click (CMD+Click on Mac OS X) or Go To Declaration (Ctrl+B / CMD
+B), PhpStorm lets us jump directly to the Blade template. Simply hovering the mouse with the Ctrl or CMD key pressed will show additional
details.
To navigate to the configuration entry's declaration, we can use Ctrl+Click (CMD+Click on Mac OS X) or Go To Declaration (Ctrl+B / CMD+B).
Hovering the mouse with the Ctrl or CMD key pressed will show additional details. If multiple declarations are found, PhpStorm will let us choose
where we want to navigate to.
Hovering the mouse with the Ctrl or CMD key pressed will show us where the translation key is defined. We can use Ctrl+Click (CMD+Click on
Mac OS X) or Go To Declaration (Ctrl+B / CMD+B) to navigate to its declaration. Typically multiple translation files will contain the same key; the
Laravel plugin will display a list of all translation files and lets us navigate to the file of choice.
When creating new classes under that folder, PhpStorm will automatically provide the namespace for that folder following the PSR-4 standard.
Read more about PHP Namespaces and PSR Support or watch the Laracasts video on PSR-4 namespacing in PhpStorm.
When @for or @foreach directives are used, variable introduction will be offered with code completion inside the construct's body.
Sections Support
While working on a Blade template, we can open a section using the @section directive. PhpStorm provides completion (Ctrl+Space / CMD+Sp
ace) for all known section names in the project.
PhpStorm also comes with an automatic code inspection that tells us when we forget to close the section using the @stop directive.
We can navigate to where the section is defined using Ctrl+Click (CMD+Click on Mac OS X) or Go To Declaration (Ctrl+B / CMD+B). Simply
hovering the mouse with the Ctrl or CMD key pressed tells us we can navigate. The Laravel plugin also adds a marker in the left-hand gutter,
which we can also click to navigate to the parent section.
Sections defined with a @section directive can be collapsed and expanded. Every HTML, JavaScript or CSS block can also be
collapsed and expanded
We can navigate to the extended or included template using Ctrl+Click (CMD+Click on Mac OS X) or Go To Declaration (Ctrl+B / CMD+B).
Hovering the mouse with the Ctrl or CMD key pressed shows us more about where we can navigate. The Laravel plugin also adds a marker in
the left-hand gutter, which we can click to navigate to the template.
When positioning the cursor on a template name in our Blade code, we can find all usages of that template by invoking Find Usages from the
context menu (Alt+F7).
Adding command line tool for Composer works in a similar way as adding support for artisan. Check the Composer Support in
PhpStorm tutorial for more information.
From the settings, we can add a new command line tool under Tools | Command Line Tool Support. The tool type is a Tool based on Symfony
Console. Next, we have to provide the path to artisan which typically is available in our project already:
PhpStorm will scan all commands exposed by artisan, including those of custom service providers we add to our project.
If a newly added service provider's commands are not available, open the settings and click the refresh button under Tools |
Command Line Tool Support. This will re-index the commands provided by artisan.
Use the Tools | Run Command... menu (Ctrl+Shift+X or CMD+Shift+X on Mac OS X) to open the command line tools console, which now
knows artisan and its various commands that are available.
Jeffrey Way has created a Composer package that adds various Laravel generators for models, views, controllers and much more. Do
give these a try as they really speed up the development process.
After installing Xdebug or Zend Profiler into our PHP runtime, we have to listen for incoming debugger connections. Use the Start Listen for PHP
Debug Connections button on the toolbar or the Run | Start Listen for PHP Debug Connections menu. Next, use the PhpStorm Debugger
bookmarklets or one of the Browser Debugging Extensions to start debugging. When a breakpoint is hit, PhpStorm will pause execution and lets
us inspect variables, the call stack, modify variables at runtime and step through code.
More information about debugging PHP code with PhpStorm is available in our Debugging PHP Applications and Zero-configuration
Web Application Debugging with Xdebug and PhpStorm tutorials.
Laracasts also has a video about debugging Laravel applications with PhpStorm that demonstrates a debugging workflow.
PHPUnit run configuration and run tests in PhpStorm. This requires PHPUnit support in PhpStorm to be configured, which is done automatically if
the Laravel project is based on any of the official Laravel Composer packages like laravel/laravel or laravel/framework.
Checkout the testing in PhpStorm video from Laracasts to see unit testing in action.
PhpStorm does not ship with database drivers installed, but it does provide a handy way of downloading them when needed: click the "
Download ... driver files" link next to the warning about missing drivers to download them.
After testing and saving the database connection details, we can explore, manage, refactor, query and code against our database. There's code
completion for tables, columns and even for generating JOIN conditions!
Learn more about Databases and SQL Editor in PhpStorm, in this tutorial.
Tweet