plugin-php
plugin-php copied to clipboard
How to install Prettier PHP Plugin in Visual Studio Code
Hello,
I am using Visual Studio Code for editing .php files. How can I set up Prettier plugin to format php in vs code. I tried to follow README.md but, I am not able to activate the code formatting on php.
Thank you for your help.
Hi @alexbalak21, can you describe the steps you took in detail? As far as I know the instructions in the Readme should still be valid.
I have the same issue, happen to be working on a large PHP codebase and the formatting is killing me, any help adding the plugin to already installed prettier in vscode?
I use Prettier to format on save on Vs Code. When I use it on php files it is not working. I installed prettier on node globally. When I use: "prettier path/to/file.php --write" it works and formats the file.
How do I add formatting php files on save ? (with Prettier PHP Plugin)
I want it to format my php files on save automatically.
For me, the plugin works as expected (tested using VScode 1.56.1 with the prettier extension v6.4.0). Try the following steps:
- Install the prettier extension for VScode (https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode)
- Install prettier and @prettier/plugin-php locally in the root your workspace:
npm install --save-dev prettier @prettier/plugin-phporyarn add --dev prettier @prettier/plugin-php - The prettier extension should ask if you want to run the local copy of prettier including all installed plugins:
If the dialog doesn't appear, try opening the package.jsonfile. - Run "Format Document" (or enable "format on save" and save the file)
Thanks, @czosel, I succeeded in installing the PHP plugin but now it doesn't actually format code but gives a response on the terminal that it has formatted the code but with no effect on the code that is recognisable as change
Hi @NateOs, can you share the detailed steps on how to reproduce this?
Great, I have prettier v6.4.0 installed, I downloaded the latest version of the PHP-plugin as at the time and installed it by "npm install --save-dev prettier @prettier/plugin-PHP I got the prompt to allow and i "allowed everywhere"
Before following your install process I had already tried and failed to install, therefore prettier will give an error message when I tried to format PHP, the error went away after installing your way, but now I don't get any actual formatting although prettier on save will tell me in the terminal that everything is ok, also I am not using any prettier config files, just the extension settings in vs code
Are you sure the file you’re trying to format is valid PHP? Can you post a screenshot?
I installed it npm install --save-dev prettier @prettier/plugin-php but it doesn't do anything.
When I choose "Format Document" from the Command Palette, VS Code pops up to say: "There is no formatter for 'plaintext' files installed."
It then opens up the Extensions market at "category:formatters plaintext".
This is for a doc that starts <?php and ends ?>, one that is picked up as PHP elsewhere...
Help.
Does the file you’re trying to format end with “.php”? Do you have the prettier extension for vscode installed?
Does the file you’re trying to format end with “.php”?
Yes. test.php.
Do you have the prettier extension for vscode installed?
Yes.
Hmm, I haven’t seen this before. Probably it would be best if you ask about this over at https://github.com/prettier/prettier-vscode
I'm a bit late to the party, but ....
- open the vscode terminal, to see if there's any output
- there's a developer console in vscode, it should show any js exceptions when the code will not format
I'm a bit late to the party, but ....
- open the vscode terminal, to see if there's any output
- there's a developer console in vscode, it should show any js exceptions when the code will not format
@UnderDogg ... I couldn't remember if this plugin was installed (doesn't appear as an extension), so (re?)-ran sudo npm install --global prettier @prettier/plugin-php...
In a PHP file, if I choose from the command palette "Format Document" (that's Prettier, right?), I still get: "There is no formatter for 'plaintext' files installed."
I have resorted to installing other PHP formatters, which correctly format the document on save. I don't know why this one won't.
Actually, I was just navigating to /usr/local/lib/node_modules/@prettier/plugin-php to delete the installed plugin and... it's not there...
@czosel The docs say to run npm install --global prettier @prettier/plugin-php. This @prettier folder is not prettier. This command creates a second folder with the @ prefix. The plugin-php is never added to prettier.
If I instead run sudo npm install --global prettier prettier/plugin-php, (no @), it still creates a folder with the @.
Is that the way this should be working?
I wouldn't know how to answer this one.
Focus on this error:
There is no formatter for 'plaintext' files installed
Check your settings, maybe .php is associated with plaintext?
Check this piece:
"files.associations": {},
Or just find plaintext in your settings.json file.
@robertandrews Yep, that's how it's supposed to be working. Inside the prettier program, all folders starting with @prettier/plugin- are searched and registered as plugins. You can verify if your installation is working by running prettier from the command line on a php file (something like yarn prettier test.php).
Installing locally and opening the package.json file worked for me. I didn't get a popup asking if I wanted to allow it everywhere though.
For me, the plugin works as expected (tested using VScode 1.56.1 with the prettier extension v6.4.0). Try the following steps:
- Install the prettier extension for VScode (https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode)
- Install prettier and @prettier/plugin-php locally in the root your workspace:
npm install --save-dev prettier @prettier/plugin-phporyarn add --dev prettier @prettier/plugin-php
Thank you. This worked for me.
Setting prettier.resolveGlobalModules to true and installing it globally (npm i -g prettier @prettier/plugin-php) fixed all my issues.
I was running into a similar issue this morning.
In my case it was because my package.json and node_modules directories were not in the root directory I had open in VS Code (they were in a subdirectory.)
I opened that subdirectory directly and Prettier started working correctly.