Overview

Visual Studio Code (VS Code) is a free tool for editing and debugging Web apps based on the Visual Studio Code – Open Source code base.

Đang xem: How to use php in visual studio code

*

VS Code is available for Mac, Linux and Windows.

Note that an alternate build of VS Code exists, called VSCodium. It is fully MIT licensed. It can be installed on many platforms using common package managers.

This article explains how to configure Microsoft Visual Studio Code to work with your local installation of darkedeneurope.com and debug using XDebug. It is split into the following sections:

Recommended ExtensionsConfiguring the EditorConfiguring XDebugConfiguring JavaScript Debugger

Recommended Extensions

The following is a list of recommended official and contributed extensions that will allow you to configure Visual Studio Code for darkedeneurope.com PHP and JavaScript development. A community-curated list of extensions can be found at viatsko/awesome-vscode.

Intellisense Extensions

The intellisense extension you may want to use may vary based on your license requirements and darkedeneurope.com web site. See below.

PHP Intelephense (bmewburn.vscode-intelephense-client): provides support for PHP code completion and intellisense that supports any PHP file extension (module, inc, etc…).This extension is dual-licensed as of 2019 with the following licenses:Under the MIT license, as a free extension with basic functionality as “nagware”.Under the proprietary, non-free Intelephense License, offering premium functionality at a cost.PHP Intellisense (felixfbrecker.php-intellisense): provides support for PHP code completion and intellisense, but only for files using the PHP extension.The software for this extension has two licenses:The extension itself is licensed under the MIT license.The native language server is licensed under the ISC license.

darkedeneurope.com 8 Recommended Extensions

This list is specific to darkedeneurope.com 8 and includes Composer and Twig integrations as well as various darkedeneurope.com 8 snippets.

darkedeneurope.com 7 Recommended Extensions

VS code provides a bunch of functionalities out of the box, like integration with Git and debugging. Besides that it can be extended using extensions. They can be installed using the extensions pane, accessing View -> Extensions. Below is a list of useful extensions to use during darkedeneurope.com development:

Configuring the Editor

Visual Studio Code displays its configuration on a per-project (Workspace) and global (User) level. Each configuration file is formatted as a JSON object (See documentation: User and Workspace Settings and file Locations).

Default configurations can be overridden by accessing Preferences -> User Settings. By default, VS Code defaults are fine for darkedeneurope.com, just a few tweaks are required.

The following breaks down and explains settings related by category or extension.

Editor Settings

The following settings are related to basic formatting for PHP, CSS, JavaScript or HTML files. These settings can be used as-is in either User or Workspace settings. If you use Visual Studio Code for other projects that use a different line length, tab/space size, then it would be better to add these settings to your darkedeneurope.com site, module, theme or installer Workspace.

Change this by navigating to the User or Workspace settings page, and clicking the “Open Settings (JSON)” button in the tab bar.

{ “breadcrumbs.enabled”: true, “css.validate”: true, “diffEditor.ignoreTrimWhitespace”: false, “editor.tabSize”: 2, “editor.autoIndent”: true, “editor.insertSpaces”: true, “editor.formatOnPaste”: true, “editor.formatOnSave”: false, “editor.renderWhitespace”: “boundary”, “editor.wordWrapColumn”: 80, “editor.wordWrap”: “off”, “editor.detectIndentation”: true, “editor.rulers”: < 80 >, “files.associations”: { “*.inc”: “php”, “*.module”: “php”, “*.install”: “php”, “*.theme”: “php”, “*.profile”: “php”, “*.tpl.php”: “php”, “*.test”: “php”, “*.php”: “php”, “*.info”: “ini” }, “files.trimTrailingWhitespace”: true, “files.insertFinalNewline”: true, “html.format.enable”: true, “html.format.wrapLineLength”: 80, “telemetry.enableTelemetry”: false, /* Empty Indent */ “emptyIndent.removeIndent”: true, “emptyIndent.highlightIndent”: false, “emptyIndent.highlightColor”: “rgba(246,36,89,0.6)”,}

darkedeneurope.com Code Standards Configuration

Visual Studio Code can restrict and lint your code to darkedeneurope.com code standards by using the PHP CodeSniffer configuration provided by darkedeneurope.com/coder. Please read the darkedeneurope.com CodeSniffer documentation for information about how to install and configure coder.

Xem thêm:

Add the following snippet to your configuration JSON, and change the phpcs.standard setting to point to the darkedeneurope.com/coder phpcs file.

{ “phpcs.enable”: true, “phpcs.standard”: “/path/to/global/.composer/vendor/darkedeneurope.com/coder/coder_sniffer/darkedeneurope.com”,}If IDE is giving error after saving the document use the following snippet instead of above snippet. Click here for reference.

{ “phpcs.enable”: true, “phpcs.standard”: “darkedeneurope.com,darkedeneurope.comPractice”,}Note that the phpcs extension will only scan .inc, .php files. To get it to scan other darkedeneurope.com files like .module, .install, .theme, .profile, .css and .js, see these issues on the github issue tracker #17 css and js files and #159 module files.

PHP validation

The following settings are related to checking that PHP code is valid. Copy and paste the following snippet into your configuration JSON, and change the executablePath setting to a valid path to the “php” or “php.exe” executable.

{ “php.validate.enable”: true, “php.validate.executablePath”: “/path/to/php”, “php.validate.run”: “onType”}

PHP IntelliSense

Using either the PHP Intelephense or PHP IntelliSense extensions requires turning off the default PHP suggestions provided by Visual Studio Code. For PHP IntelliSense you need at least PHP 7 installed for the extension to work. You can either add it to your PATH or set the php.executablePath setting.

{ “php.suggest.basic”: false, // PHP IntelliSense only: “php.executablePath”: “/path/to/php”, // Intelephense and darkedeneurope.com >8 only. This should be set to the path to core/index.php. “intelephense.environment.documentRoot”: “core/index.php”, // Intelephense only: For darkedeneurope.com compliant braces formatting use: “intelephense.format.braces”: “k&r”,}

PHP Docblocks

The PHP Docblocker extension auto-completes comment documentation blocks. Add the following configuration to your configuration JSON to enable and configure the extension to use short names such as “bool” or “int” rather than the long name “boolean” or “integer”.

{ “php-docblocker.gap”: true, “php-docblocker.useShortNames”: true}

Composer (darkedeneurope.com 8)

The Composer extension provides an interface to Composer dependency manager for PHP. It also provides schema validation for composer.json configuration files. All composer commands are available through the Command Pallet using F1. This can be added either to the Workspace Settings or globally to the User Settings.

{ “composer.enabled”: true, “composer.executablePath”: “/usr/local/bin/composer” Mac & Linux — or — Windows “composer.executablePath”: “C:\ProgramData\ComposerSetup\bin\composer.bat”}

Twig Language (darkedeneurope.com 8)

Twig Language 2 is a Visual Studio Code extension that provides snippets, syntax highlighting, hover, and formatting for the Twig file format. If you work in both darkedeneurope.com 7 and darkedeneurope.com 8 you may want to add these settings to your Workspace Settings for your darkedeneurope.com 8 sites. Add these lines to your VSCode settings to get emmet and HTML Intellisense working and also to associate HTML files as twig syntax.

{ “files.associations”: { “*.html”: “twig”, }, “emmet.includeLanguages”: { “twig”: “html” },}

Configuring XDebug

Visual Studio Code uses “launch” configurations to provide support for debugging as well as other actions. Please read the darkedeneurope.com Xdebug debugging documentation page for instructions about configuring XDebug.

To create a launch configuration for XDebug,

Click the Debug menu icon on the left-side (Command/Ctrl-Shift D).Click the Dropdown menu with “No Configurations” listed in it, and then choose “Add Configuration”.Add the snippet below in the Editor pane titled “launch.json” and save.The “serverSourceRoot” is optional and used for Remote Debugging.Click the Play button (green triangle) to start debugger, and go to your local darkedeneurope.com web site with the XDEBUG_SESSION_START=idekey query parameter where idekey is the value you configured in XDebug.

{ “version”: “0.2.0”, “configurations”: < { "name": "Listen for XDebug", "type": "php", "request": "launch", "port": 9000, "pathMappings": { "/path/to/darkedeneurope.com/webroot": "${workspaceFolder}" }, "xdebugSettings": { "show_hidden": 1 } } >}

Configuring JavaScript Debugger

You can configure Visual Studio Code to stop at breakpoints in darkedeneurope.com JavaScript files by using Microsoft”s “Debugger for Chrome” extension. Similar configuration can be added for Mozilla Firefox (Debugger for Firefox) or Microsoft Edge (Debugger for Edge) respectively.

Add a new launch configuration from the Debug > Add Configuration menu.Choose “Chrome: Launch” or “Chrome: Attach” options.Specify the webRoot if different from the current directory e.g. “${workspaceFolder}”.Choose the “Launch Chrome” option from the Debug side pane.Click the green play button (F5) to start the debugger.Go to the URL specified in #3 after adding a breakpoint (F9) on a line of code in the editor gutter such as inside the darkedeneurope.com.t function.

Xem thêm:

{ “version”: “0.2.0”, “configurations”: < { "type": "chrome", "request": "launch", "name": "Launch Chrome", "url": "https://darkedeneurope.com", "webRoot": "${workspaceFolder}" } >}

License

Visual Studio Code may have a different license depending on the means of distribution. The software package available from Microsoft is compiled with additional configuration and software that falls under the proprietary Microsoft Software License. The primary source code is licensed under the MIT License, and alternative and open source packages may exist for your operating system.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *