Start PHP Server using Context Menu - Windows
There is a reason why computers now have full Graphic User Interfaces (GUI’s) and no longer only have shells and terminals. But for some software, you still need to open up a terminal or command prompt to start the software. Or do you?
Today we are going to look at adding a Context Menu item inside of Windows 8.1, but it will work in other versions of windows such as XP, Vista, 7 and 8. A Context Menu, if you are unaware of what it is, is the menu that appears when you right click on a program, folder, desktop or other item on your computer. It’s provides optional features that you can use, such as opening a file in a different program or to archive a folder.
The context menu we are going to add is to start the PHP Web Development Server. I created a video on how to setup a PHP Web Development Server on YouTube which you will need to watch if you are unsure how to setup the PHP Server.
Creating The Batch File
The first step to this tutorial is to create a batch file. A batch file is an executable file that contains command prompt commands. Creating these files are fairly easy and you don’t need any third-party software.
To create a batch file, you will want to open up Notepad or any type of
“programmer notepad”, such as Geany, Sublime Text, Notepad++. Then you
will want to save a new document with the file extension of .bat
. If
the file extension isn’t .bat
the rest of the tutorial won’t work
properly.
Alternatively, you can right click on your desktop, or inside of a
folder, and click on “New > Text Document”. If you have file extensions
visible, you will be able to rename the file extension to .bat
.
Inside of your Batch file, you will want to enter the following text. Double clicking the batch file will not open it in notepad. You will need to right click the batch file and click on “Edit” or use the Open option inside of Notepad.
@echo off
php -S localhost:80 -t "%1"
The first line of this batch file hides the initial input commands. The
second line is to start our PHP server. We added the option -t "%1"
to specify the directory that the server should use as the root. The
%1
is the first argument that is passed to the batch file, which will
be the folder we are right clicking on.
Adding the Context Menu
Now we are able to add the Context Menu by modifying the registry. When working in the registry you should know that making a mistake can cause damage to your Operating System. The main thing you should keep in mind when editing the registry is do not Delete or Modify existing keys and values. By following that simple rule you won’t run into any issues.
You will want to open up the Registry Editor by running “regedit” from
the Run Dialog. You can open the Run Dialog by press ctrl + R
on your
keyboard.
- Navigate to the location of
HKEY_CLASSES_ROOT\Directory\shell
. - Right click on
shell
and click onNew > Key
. You can name this key whatever you want, but I suggest naming it “php” or similar. - Click on the new key you created and in the right panel double click on “(Default)” to edit the value. You will want the value to be the name of the option in the context menu, such as “Start PHP Server”.
- Right click on the new key you created and create another key called “command”.
- Click on the “command” key you created and then double click on the “(Default)” key in the right panel. You will want to change this value to the full file path of the batch file you created. For example, if your batch file is on your desktop, it will be at
C:\Users\USERNAME\Desktop\phpserver.bat
. After the file path, you will want to add"%1"
with a space separating them.
If you now right click on any folder on your computer, you should see your new item that will let you launch a PHP server that uses the selected folder as the root directory. This can also be applied to all sorts of other programs as well.