PhpStorm 2018.2 Help

Laravel

Laravel is a free, open source PHP web application framework. It is built on top of several Symfony components, and makes common tasks such as authentication, routing, sessions and caching much easier to implement.

Laravel support is provided by means of the Laravel plugin and the Laravel IDE helper.

Before you start

Before you start working with Laravel, make sure that Laravel plugin is installed and enabled.

Additionally, make sure Composer is installed on your machine and initialized in the current project as described in Composer Dependency Manager.

To perform initial configuration

  1. Install Laravel IDE helper generator with Composer. To do this, open Tools | Composer | Manage Dependencies and install the barryvdh/laravel-ide-helper package. Refer to Installing a specific package for details.

  2. Add Laravel IDE helper as a ServiceProvider into the application. In the app/config/app.php file, add Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class under the providers element:

    <?php return array( // ... 'providers' => array( // ... 'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider', // Laravel IDE helper ), // ... );

  3. Enable command line tool support for artisan:

    • In the Settings/Preferences dialog (Ctrl+Alt+S), add a new command line tool under Tools | Command Line Tool Support.

    • Set the tool type to Tool based on Symfony Console.

    • Provide the path to artisan:

      ps laravel artisan cli tool

    You can now use artisan from within PhpStorm.

  4. Open the Command Line Tools Console Tool Window by either selecting Tools | Run Command... from the main menu or pressing Ctrl+Shift+X.

    Run the artisan ide-helper:generate command to generate the required PHPDoc information.

    ps laravel artisan cli run generate

    PhpStorm and the Laravel plugin will use this information to provide code completion and navigation.

The Laravel IDE Helper may have to be run after changing or adding services, controllers, models and views. Alternatively, set up File Watchers in PhpStorm to automatically regenerate this file when, for example, composer.json is updated.

You can also install the Laravel generators Composer package to add various Laravel generators for models, views, controllers, and much more.

Code completion and navigation

The Laravel plugin provides code completion and navigation for various Laravel components: controllers, routes, views, configuration, services, and translations.

Code completion

In the editor, press Ctrl+Space to invoke code completion and do any of the following:

  • Reference a controller when using the Redirect and Route facade's various functions:

    ps laravel complete controller
  • Reference a Blade template (or view) when using the View facade:

    ps laravel complete view
  • Reference various keys that are defined in our application's settings when using the Configuration facade:

    ps laravel complete config
  • Complete various translation keys when using the Lang and calling Lang::get():

    ps laravel complete translation

Code navigation

To navigate to the declaration of an item, position the caret at its usage and press Ctrl+B. Alternatively, Ctrl+Click (for Windows and Linux) or ⌘+Click (for macOS) the usage.

  • Navigate to the controller's declaration:

    ps laravel navigate controller
  • Navigate to a Blade template (or view) declaration:

    ps laravel navigate view
  • Navigate to the declaration of a configuration entry or a service:

    ps laravel navigate config
  • Navigate to the declaration of a translation key:

    ps laravel navigate translation

Blade template support

PhpStorm provides syntax highlighting for Blade templates files. It highlights various Blade syntax constructs, as well as any HTML, JavaScript and CSS code inside the templates.

Next to syntax highlighting, PhpStorm provides several other Blade-specific functions.

Code completion for braces and directives

PhpStorm's editor provides code completion both for standard and custom Blade directives, which can be defined In the Settings/Preferences dialog (Ctrl+Alt+S) under Languages & Frameworks | PHP | Blade.

ps laravel complete blade directive

When @for or @foreach directives are used, variable introduction with code completion is available inside the construct's body.

ps laravel complete variable

Sections support

While working on a Blade template, you can open a section using the @section directive. PhpStorm provides code completion (Ctrl+Space) for all known sections' names in the project.

ps laravel complete blade section

PhpStorm provides the code inspection that detects the sections that are not closed using the @stop directive.

ps laravel blade closed section inspection

To navigate to the declaration of a section, position the caret at its usage and press Ctrl+B. Alternatively, Ctrl+Click (for Windows and Linux) or ⌘+Click (for macOS) the usage.

The Laravel plugin also adds a marker to the editor gutter, which lets you navigate to the parent section.

ps laravel blade gutter parent section

Code completion and navigation for extends and includes

Blade templates are often composed of various includes of small, reusable blocks which are in turn other templates. You can also extend templates and provide content for additional sections. PhpStorm and the Laravel plugin provide completion for template names in both the @extends and the @include directives. Completion results will include template directory names as well as full template names which we can select from.

ps laravel blade extends completion

To navigate to the declaration of a template, position the caret at its usage and press Ctrl+B. Alternatively, Ctrl+Click (for Windows and Linux) or ⌘+Click (for macOS) the usage.

Generating code with Live Templates

PhpStorm provides numerous code generation facilities. After downloading and installing the PhpStorm Laravel Live Templates, you can extend the standard live templates set with Laravel-specific live templates, such as:

  • Blade directives

  • Input and Request snippets

  • Cookie snippets

  • Route snippets and generation

  • View, Response and Redirect templates

  • Building schema (includes column types)

  • Cache

  • Form and session snippets

  • Snippets calling various helpers

Last modified: 10 October 2018

See Also