Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

VSCode for PHP and Laravel

Posted on Aug 21 This post should help you setup Visual Studio Code to use for PHP and Laravel development. It it a solid base configuration that can be expanded upon using additional workspace specific configurations. I will cover the best extensions to use as well as some helpful configuration settings and external tools.Let’s get started with the most important extensions!This is the most important extension to install for PHP support. It provides a fast language server that adds code completion, go to definition, formatting, and more. You can also purchase a license at Intelephense, which I highly recommend. It adds some additional features like renaming symbols and other code actions.Once installed, disable the built-in PHP features so Intelephense is used instead:If a license was purchased, use cmd+shift+p to bring up the Command Palette and search for “Enter licence key”.For the most part, the default settings are fine for Intelephense. At a workspace level, setting the document path and PHP version can be helpful if working on multiple PHP projects and frameworks.Finally, if Intelephense will be used for formatting (versus an external tool like PHP CS Fixer or Pint), then set it as the default formatter for PHP files. This is done by pulling up the JSON version of the settings.This is both an external tool and an extension. However, the extension is not available in the VSCode extension marketplace and needs to be installed manually. The extension provides a lot more helpful code actions like replacing a qualifier with an import and adding return types.First, install Phpactor by using the instructions or following the steps below:With Phpactor installed on the system, next is to install the plugin. Download the latest release (phpactor.vsix). Then install in VSCode either by importing the plugin or using the CLI.With the plugin installed, Phpactor will index the workspace and then add new code actions. The screenshots below show replacing a qualifier with a namespace and adding return types.This plugin adds additional autocompletion for things like views and routes. For example, in the routes/web.php file, a list of available views displays in the autocompletion popup:It also adds autocompletion for Laravel validation rules and configuration.This plugin is used to quickly navigate to view, config, and other files in a Laravel application just be hovering over the string. For example, in the routes/web.php file again, when hovering over “welcome” and popup appears to navigate directly to the welcome.blade.php file. The opt+; shortcut can also be used to navigate to the file.This plugin adds syntax highlight for .blade.php files as well as helpful snippets. It can even be used to format Blade files if desired. However, later in this post, Prettier will be setup to format Blade files.These plugins provide easy keybindings for running tests. If the project uses Pest, use the Better Pest extension, otherwise, use Better PHPUnit. These plugins can also be enabled/disabled specifically for workspaces as well if using multiple projects.The following keybindings are provided by default to run a specific test depending on the cursor location, or run all the tests in the file, or the previously run test(s).Refer to the docs for additional settings to setup with Docker or other more advanced configurations.Prettier is an opinionated framework for formatting JavaScript/TypeScript files. It is highly recommended for formatting React and Vue files if using something like Inertia. It can also format other filetypes, like JSON and Markdown. After installing the extension, also install Prettier into the project:Once installed, the command palette (cmd+shift+p) can be opened and use the Prettier: Create Configuration File to create a .prettierrc file in the project. See the following for a list of configurable rules: Options · Prettier.Additional plugins can be added to support more features and languages for Prettier as well, such as Blade support. Install the shufo/prettier-plugin-blade plugin and add it to the config file:It can even sort Tailwind CSS classes in Blade files by using the sortTailwindcssClasses option. Look at the Github repo for additional settings.To set the Prettier as the default formatter for various file types, use the following settings in VSCode:Though this is not an extension for VSCode, the Laravel IDE Helper greatly improves the development experience in the editor. It generates helper files for Laravel applications to have better autocompletion support for various magic methods in Laravel that Intelephense cannot resolve. It can analyze models, migrations, facades, and more. Install it with composer.Once installed, new Artisan commands are available to generate the helper files. The following are the most useful.For the models generation, it is possible to add PHPDocs directly to the model files or use an external file. The former tends to be more accurate but adds a lot of comments to the files. The latter is cleaner but sometimes go to definition goes to the generated file versus the actual model. Decide what works best for the project.When the commands are run, VSCode can now autocomplete fields on models, like the email field on the User model below.This can be made even simpler by adding some scripts to the composer.json file. First, create an ide-helper script to run the various commands. The script below writes the PHPDocs externally for the models versus on the models themselves.Next, update the post-update-cmd script. This will automatically run the ide-helper script when running composer update.To take this one step further, VSCode tasks can be configured to run this command from within VSCode and even assign it to a shortcut.To create the tasks.json file, use cmd+shift+p to open the command palette, the search for Tasks: Configure Task. Inside the menu select the Create tasks.json file from template, then select Others. This creates a tasks.json file inside the .vscode directory in the project. In the tasks.json file, add the following:Now this command can be run from the command palette by going to Tasks: Run Task.Finally, to add a keyboard shortcut for the tasks, add the following to the keybindings.json:Now clicking cmd+shift+. will quickly run the IDE helper.I hope this guide will help make you a much more efficient Laravel developer. Visual Studio Code is a powerful editor and with the right extensions and configuration is an excellent editor for PHP and Laravel development. It is not quite as powerful as PhpStorm (especially with the Laravel Idea package) and requires more work to configure. However, for a free to use editor, it is tough to beat. Though I highly recommend PhpStorm, I know a lot of developers have experience with VSCode and maybe doing more than just PHP development so the change it not worth it.Please let me know in the comments if there’s anything else you would like me to cover or expand on, like Laravel Sail support or additional tools like Phpstan/Larastan. Also, let me know if there’s any other helpful extensions or configurations I might have missed.Finally, below I provided an example of the settings I use in VSCode.Here’s an example of the settings I use in VSCode for PHP and Laravel development. I also make heavy use of workspace specific settings to further customize my setup for individual projects.Templates let you quickly answer FAQs or store snippets for re-use.I really enjoyed the effort put into this article. Actually quite impressed at the level of code completion offered through the various plugins. The examples were laid out nicely and I appreciate seeing a full settings file available at the end. Thanks for the great article! Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Abssi Franki - Jul 28 Nevo David - Aug 8 June Dang - Jul 24 MrChoke - Jul 23 Once suspended, sean_kegel will not be able to comment or publish posts until their suspension is removed. Once unsuspended, sean_kegel will be able to comment and publish posts again. Once unpublished, all posts by sean_kegel will become hidden and only accessible to themselves. If sean_kegel is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Sean Kegel. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag sean_kegel: sean_kegel consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging sean_kegel will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

VSCode for PHP and Laravel

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×