PHP PORTABLE IDE: Run Menu
US PHP IDE : Introduction | Notepad++ | Install PHP | NppExec Config | PHP extensions | Run Menu | XDebug 1 | Debugging 1 | XDebug 2 | Debugging 2
|
|
Uniform Server Portable PHP IDE. |
Notepad++ Run Menu
The following page covers how to edit a PHP web page and run it on a local web server using Notepad++.
Examples assume you are running Uniform Server as the local server and pages are located in the root folder www (or its sub-folders). These pages are directly edited and run on the server using Notepad++. This configuration is intended for running small test code snippets on a working server.
Previous page covered a browser redirection example this will be integrated into Notepad++ providing additional automation for testing PHP pages. The only debugging feature included is a PHP syntax check. That said it is easy to include XDebug this is covered latter.
Intention is to demonstrate how easy it is to use PHP CLI to script additional feature for Notepad++
PHP Syntax check
After editing a PHP script always run a syntax check. If PHP is installed on your PC as a separate application the command line has the following format:
- php -l filename
- For example php -l test.php - If syntax is correct you will see a message: No syntax errors detected in test.php
Any syntax errors will be listed with the following format:
- Parse error: syntax error, unexpected T_STRING in test.php on line 4
- Errors parsing test.php
When running PHP as a portable installation above format changes slightly, you must specify a full path to PHP.
The above format is similar to running a PHP CLI script on Notepad++ as covered on previous page.
The NppExec command script used is reproduced below:
NPP_SAVE cls $(NPP_DIRECTORY)\php\php.exe -n $(FULL_CURRENT_PATH) |
- Save current file |
Only a small change is required for syntax checking only.
NPP_SAVE cls $(NPP_DIRECTORY)\php\php.exe -ln $(FULL_CURRENT_PATH) |
- Save current file |
Parameter -n informs the PHP interpreter not to use a configuration file. While -l (lower case letter) performs a syntax check only and does not run the script.
Add new NppExec command script
Add the new script as follows:
Add new command script
Opens Script name side window:
|
|
Note: With no script open following message is produced C:\us_notepad\unicode\php\php.exe -ln new 1 Process started >>> Could not open input file: new <<< Process finished. ================ READY ================ |
The following assigns our two command scripts to menu items.
The above allows you to perform a syntax check on a PHP script displayed in Notepad++. You can run a check on either CLI or web-scripts PHP makes no distinction between the two.
Run PHP web page on server
On the previous page a simple redirection HTML script was used for redirecting a users default browser to run a PHP page on a local server.
For our IDE we want to edit PHP pages using Notepad++ and have current displayed page run on a local server using a menu item.
Notepad++ has a run drop-down menu where new menu items are inserted. Each menu item has the capability of running a single line command. Hence we can create a new item that runs an external PHP CLI script. This script in turn runs the redirection HTML script.
Redirect HTML page
From a scripting point of view we have two choices either use an existing file or create one on the fly as required. Using an existing file requires it to be updated before use; this involves loading, search, replace and save. Creating on the fly requires only saving the new file before use. It also has one other advantage a user can inadvertently delete it with no adverse consequences since the page is automatically recreated.
Redirect HTML page Note: The url includes a port number although :80 can be omitted it is shown to highlight that a different port can be used. Hence use a variable in the PHP code. |
<html> <head> <meta http-equiv="refresh" content="1;url=http://localhost:80/index.php"> <title>Uniform Server Redirect</title> </head> <body> </body> </html> |
PHP CLI redirect script
Using above we can write a small PHP redirect script.
- Using Notepad++ create a new file with content shown below
- Save to folder C:\us_portable_ide\unicode with name redirect.php
<?php /* ############################################################################### # Name: redirect.php # Developed By: The Uniform Server Development Team # Modified Last By: Mike Gleaves (Ric) # Web: http://www.uniformserver.com # V1.0 18-11-2010 ############################################################################## */ // Args passed $(NPP_DIRECTORY) $(FILE_NAME) $domain_name = "localhost"; // Domain name default localhost $port = "80"; // Get server port default 80 $file_name = "redirect.html"; // File to create for redirect if($argc != 3){ // Are two args passed exit; // No: Give up } $npp_root = str_replace('\\', '/', $argv[1]); // Notepad++ root folder $page = $argv[2]; // Page displayed in Notepad++ $redirect_array = array(); // Create new array and reset $redirect_array[] = '<html><head>'; $redirect_array[] = '<meta http-equiv="refresh" content="1;url=http://'.$domain_name.':'.$port.'/'.$page.'">'; $redirect_array[] = '<title>Uniform Server Redirect</title>'; $redirect_array[] = '</head><body></body></html>'; $string = implode("\n",$redirect_array); // Convert array to string file_put_contents("$npp_root/$file_name",$string); // Save string to file // Run option 1 - For winbinder test only wb_exec("$npp_root/$file_name"); // Run redirect file // Run option 2 - Use when WinBinder not installed //$cmd= "$npp_root/$file_name"; // Command to run //$WshShell = new COM("WScript.Shell"); // Open com object //$oExec = $WshShell->Run($cmd); // Run command ?> |
Edit Notepad++ file shortcuts.xml
We want to run the above script using the run drop-down menu. Editing file C:\us_portable_ide\unicode\shortcuts.xml and adding an appropriate line inserts a menu item. However this file cannot be directly edited using Notepad++ it is overwritten with original values when Notepad++ is closed.
Solution is to copy file to a different location perform all edits. Close Notepad++ copy file back to original location and restart Notepad++ to pick-up new file.
File content
Following shows content of file shortcuts.xml with the exception of the line to added I have removed all other lines from <UserDefinedCommands> section.
For clarity line to add has been split it should be on a single line placed at top of <UserDefinedCommands> section.
<NotepadPlus> <InternalCommands /> <Macros> <Macro name="Trim Trailing and save" Ctrl="no" Alt="yes" Shift="yes" Key="83"> <Action type="2" message="0" wParam="42024" lParam="0" sParam="" /> <Action type="2" message="0" wParam="41006" lParam="0" sParam="" /> </Macro> </Macros> <UserDefinedCommands> <Command name="Run PHP script on server" Ctrl="no" Alt="no" Shift="no" Key="0">$(NPP_DIRECTORY)\php\php-win.exe -c $(NPP_DIRECTORY)\php\php_cli_ide.ini $(NPP_DIRECTORY)\redirect.php $(NPP_DIRECTORY) $(FILE_NAME)</Command> </UserDefinedCommands> <PluginCommands> <PluginCommand moduleName="NppExec.dll" internalID="20" Ctrl="no" Alt="no" Shift="no" Key="39" /> </PluginCommands> <ScintillaKeys /> </NotepadPlus>
Command Line breakdown
<Command | - Open tag for command |
name="Run PHP script on server" | - Name to display in menu item |
Ctrl="no" Alt="no" Shift="no" Key="0"> | - Shortcut key to use in this example none |
$(NPP_DIRECTORY)\php\php-win.exe | - Full path to PHP application |
$(NPP_DIRECTORY)\redirect.php | - Full path of script to run |
$(NPP_DIRECTORY) | - First parameter passed to script |
$(FILE_NAME) | - Second parameter passed to script |
</Command> | - Close tag for command |
How to edit shortcuts.xml
- Copy file C:\us_portable_ide\unicode\shortcuts.xml to folder C:\us_portable_ide\php_scripts
- Start Notepad++
- Open file C:\us_portable_ide\php_scripts\shortcuts.xml
- Add the above line to top of <UserDefinedCommands> section
- Save file and close Notepad++
- Copy file C:\us_portable_ide\php_scripts\shortcuts.xml to folder C:\us_portable_ide\unicode
- Start Notepad++
Test
Following test assumes you have Uniform Server running and extracted to folder C:\Nano_5_6_15\z_test\UniServer substitute your paths accordingly.
|
<?php print "Hello world"; ?> |
Summary
Above has shown how to perform PHP syntax checks, run scripts on a local server and how to configure Nptepad++ menu options.
The above is suitable for testing PHP CLI scripts and small PHP web page snippets.
Next page shows how to add XDebug this greatly increases flexibility of our PHP IDE.