[Php Tutorial 2024] Lession 01 - Setup Php Development Environment in Windows 10

[Php Tutorial 2024] Lession 01 - Setup Php Development Environment in Windows 10

Post Date : 2024-03-28T00:24:54+07:00

Modified Date : 2024-03-28T00:24:54+07:00

Category: php-tutorial

Tags: php

A very long story that every body knows is on window XAMPP/WAMP were the most popular choices in the past. But one day i’ve figure out Laragon - the all in one and light tool for windows. I never came back! Here is your new story.

Setup Tools for development

  1. Install PHP Environment -> https://laragon.org/index.html
  2. Install and Update composer -> https://getcomposer.org/download/
  3. VsCode Extensions for PHP
  4. Debug PHP with VSCode and Xdebug

Install PHP Environment

Install Laragon in Windows

Download and install the appropriate versions

image

Then launch laragon. Your www root directory will be placed in “C:\laragon\www”

When you create any folder in this www root directory, when launch wih laragon, it may help you to run at [your-folder-name].test

With Laragon, you can also manage multiple php versions on Window.

Wanna use another version php with laragon

Find your specific version at php archives Usually, we use safe thread version so, for example, i wanna run with php 7.4.x

9/29/2022 5:50 PM 26238674 php-7.4.32-Win32-vc15-x64.zip => this is my expected version

Just download then extract and move it into “C:\laragon\bin\php”

And select your appropriate version

image

Remember to restart and check your version with command line like this

php -v

Install and Update composer

Laragon already included a built-in composer version. But you may wanna upgrade it, so just go to “C:\laragon\bin\composer”, and run these commands

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Then check your composer version

composer --version

VsCode Extensions for PHP

image

  • PHP Intelephense
  • PHP Namespace Resolver
  • PHPDoc Comment
  • PHP Debug

Install XDebug on Windows

  • Create index.php at your root folder
  • Edit content and replace with
<?php
phpinfo();
  • View source and copy all of content
  • Paste in this xdebug wizard form
  • Follow the instruction
  • Then enable xdebug extension witht laragon or edit your php.ini

image

image

Then edit your php.ini, in my example “C:\laragon\bin\php\php-7.4.32-Win32-vc15-x64\php.ini”, and add the following lines at the end of file.

[XDEBUG]
xdebug.mode = coverage,debug,develop
#xdebug.client_host = 127.0.0.1
xdebug.client_port = 9003
xdebug.start_with_request=yes

Then create new debug config on vscode, you may see something like this

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Listen for Xdebug",
      "type": "php",
      "request": "launch",
      "port": 9003
    },
    {
      "name": "Launch currently open script",
      "type": "php",
      "request": "launch",
      "program": "${file}",
      "cwd": "${fileDirname}",
      "port": 0,
      "runtimeArgs": ["-dxdebug.start_with_request=yes"],
      "env": {
        "XDEBUG_MODE": "debug,develop",
        "XDEBUG_CONFIG": "client_port=${port}"
      }
    },
    {
      "name": "Launch Built-in web server",
      "type": "php",
      "request": "launch",
      "runtimeArgs": [
        "-dxdebug.mode=debug",
        "-dxdebug.start_with_request=yes",
        "-S",
        "localhost:0"
      ],
      "program": "",
      "cwd": "${workspaceRoot}",
      "port": 9003,
      "serverReadyAction": {
        "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
        "uriFormat": "http://localhost:%s",
        "action": "openExternally"
      }
    }
  ]
}

Press F5 to debug your website

image

Now when you access your website vscode will jump in the selected breakpoint you set before.

Feel free to working with PHP on window 10

Add Cmder Terminal in your VsCode

Open File > Preferences > Settings Then search “terminal profile window”

image

Add the following lines into settings.json

settings.json

  "terminal.integrated.profiles.windows": {
    "Cmder": {
      "name": "Cmder",
      "path": [
          "${env:windir}\\Sysnative\\cmd.exe",
          "${env:windir}\\System32\\cmd.exe"
      ],
      "args": ["/k", "C:\\laragon\\bin\\cmder\\vendor\\bin\\vscode_init.cmd"],
      "icon": "terminal-cmd",
      "color": "terminal.ansiGreen"
    }
  }

If you wanna change “Cmder” as default profile terminal

 "terminal.integrated.defaultProfile.windows": "Cmder",

If you are fan of PHP Webstorm, no worries

File -> Settings -> Tools -> Termninal

image

“cmd.exe” /k “"%CMDER_ROOT%\vendor\init.bat”"

CMDER_ROOT should be “C:\laragon\bin\cmder” where you installed lagon