PHP PORTABLE IDE: Run Menu

From The Uniform Server Wiki
Jump to navigation Jump to search

 

MPG UniCenter

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
- Clear console window
- Run currently displayed PHP script

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
- Clear console window
- Run syntax check on currently displayed PHP script

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.

Top

Add new NppExec command script

Add the new script as follows:

Add new command script

  • Press key F6
  • Step A) From drop-down menu select <temporary script>
  • Step B) Enter list of commands
  • Step C) Click save button

Opens Script name side window:

  • Step D) Enter a name for command script php_syntax_check
  • Step E) Click save

Ide php run 1.gif

  • Click OK - Saves and runs script.

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 ================

Ide php run 2.gif

Top

Assign command scripts to menu items

The following assigns our two command scripts to menu items.

  • Open NppExec Advanced Options Plugins>NppExec>Advanced Options..
  • Step E) Check box Place to the Macro submenu
  • Step F) From the drop-down menu select command script php_syntax_check
  • Step G) Enter Item name Check PHP Syntax
  • Step H) Click Add/Modify Menu Items are automatically inserted.
  • Step I) Click OK and Restart Notepad++.

Note 1:

Check new menu items added to macro drop-down menu as shown below.

Ide php run 4.gif
Only two mouse clicks macro>php_syntax required to run a PHP syntax check

Ide php run 3.gif

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.

Top

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>

Top

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
?>

Top

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

Top

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++

Top

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.

  • Start Notepad++
  • Create a new script for example that shown on the right
  • Save to folder C:\Nano_5_6_15\z_test\UniServer\www with name hello.php
  • From Notepad++ Run menu click on item Run PHP script on server
  • Default browser opens and redirected to page http://localhost/hello.php
<?php
print "Hello world";
?>

Top

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.

Top