chcschmid@gmail.com's avatar

PHPStorm + Blade help: Automatically insert "use" FQN instead of FQN

When working inside blade files, I start typing out a class name, and it brings up the list of matching classes. I select one, and then it automatically inserts the FQN. I don't mind this inside {{ }}, but I don't like it much when I am inside a php block (@php)

Is there anyway to accomplish this with PHPStorm settings?

What I would like:

@php

use Livewire\Volt\Component;

new class extends Component { ... }

What it currently does:

@php

new class extends \Livewire\Volt\Component { ... }
0 likes
5 replies
LaryAI's avatar
Level 58

Unfortunately, PHPStorm does not have a built-in feature that automatically adds the use statement for a class when you're working inside a Blade file's @php directive. The IDE typically manages imports and fully qualified names (FQN) in PHP files, but Blade files are a bit different due to their nature of mixing HTML and PHP.

However, you can manually add the use statement at the beginning of your Blade file within the @php directive. PHPStorm will then recognize the imported class, and you can use the short name within the rest of your @php block.

Here's how you can manually add the use statement:

@php
use Livewire\Volt\Component;

new class extends Component { ... }
@endphp

After adding the use statement, PHPStorm should provide autocompletion for the Component class without the FQN within the @php block.

If you find yourself frequently needing to add use statements in Blade files, you might consider creating a Live Template in PHPStorm to speed up the process. Here's how you can create a Live Template:

  1. Go to Preferences or Settings in PHPStorm.
  2. Navigate to Editor > Live Templates.
  3. Click the + icon to add a new template.
  4. Choose the applicable context for Blade files.
  5. Define a template abbreviation and description.
  6. Enter the template text, for example:
    @php
    use $CLASS$;
    
    $END$
    @endphp
    
  7. Define the variables ($CLASS$ and $END$) if necessary.
  8. Save the template.

Now, when you type the abbreviation you defined in a Blade file and press the tab key, PHPStorm will insert your template, allowing you to quickly add a use statement.

Remember that Blade files are compiled into cached PHP files, so any PHP code you write inside @php directives should be valid PHP code. Adding use statements at the top of these blocks is perfectly acceptable and will be understood by the PHP engine when the Blade template is rendered.

kiwi0134's avatar

You might want to consider refactoring your code. Your blade views should contain near to no logic at all. They're views. By definition responsible for displaying data. Instead, you should pass your data from the component class, controller, livewire component or wherever you're rending this view from.

What are you trying to achieve, why do you want to have code blocks inside your blade view?

nexxai's avatar

@kiwi0134 You should go read up on Volt. The entire point is to collocate certain logic directly within the Volt component.

2 likes
kiwi0134's avatar

@nexxai I didn't realize OP is using Volt. Ignore my comment in this case. Sorry.

Intr0spect1ve's avatar

This actually is the way Laravel Volt is designed to work. So I have the exact same problem.

Please or to participate in this conversation.