3

I'm yet a newbie to PHP development, so far I used NetBeans for the job. Unfortunately NetBeans is not the best IDE, and it is unreasonably slow on my Mac. I'd like to use Eclipse PDT for PHP, as I know and like Eclipse a lot better (I'm coming from Java).

But I cannot set up a server in Eclipse... All docs and topics just showed ppl saving files in the htdocs folder of an external server (such as MAMP or XAMPP). As I don't need a database, I just want to use PHP's built-in server instead of installing and running a heawyweight app in vain.

I'd like to reproduce the only really good thing in NetBeans: I just click on the Run button, and I see the result in the Browser immediately. How do you set that up?

Zach J
  • 81
  • 2
  • 6

2 Answers2

4

Even if I am a bit late to help you, I want to write down my solution, cause I've faced the same problem today.

I think the only chance is to start the php built-in webserver manually. Open a terminal in the desired root directory and start the webserver with

php -S localhost:8000

Then you can add a new server with Base URL: http://localhost:8000 and the choosen document root and you'll have the same functionality like in Netbeans.

  • Thanks Tobias, i found only this solution myself. Lighter on resources, but not so fast and easy. I know, i want too much, but once you got the good stuff, less is not enough anymore :) – Zach J Dec 06 '16 at 22:50
  • 1
    Yes, this allowed me to finally run my codeigniter test installed with the eclipse built in composer – Robert Sep 14 '19 at 06:52
1

Put together this hackety-hack-hack to make this work (even works with xdebug remote debugging if you set it up!!!).

UPDATE: one caveat with this solution is when you terminate the running CLI in Eclipse, it's terminating the wrapper script, not the php server directly. I've added some trapping and forawding of signals to child (php server) process. Works in OSX.

Overview:

  • I'm running Eclipse Neon
  • Need a router file in document root you wish to serve from (see this: http://php.net/manual/en/features.commandline.webserver.php)
  • Create a wrapper bash script to call PHP in server mode and pass in details
  • Set script to have executable permissions
  • Add this bash script as a PHP executable
  • For the project, create a run configuration as PHP CLI, using this new executable, passing the router file in.

Here's the bash script php5.6-server:

#!/bin/bash

_sigterm() {
   echo "Caught SIGTERM signal!"
   kill  -2 "$child"
}

_sigint() {
   echo "Caught SIGINT signal!"
   kill  -14 "$child"
}

if [ $1 = "-v" ]; then
   #This is needed for when eclipse trys to detect php version
   /path/to/php -v
else
   trap _sigterm SIGTERM
   trap _sigint SIGINT
   # This is why your router file needs to be in the doc root
   ROUTER=${@: -1}
   DIR=$(dirname $ROUTER)
   /path/to/php -S  localhost:8000 -t $DIR $ROUTER
   child=$!
   wait "$child"
fi

Here's a simple router.php just to get it working:

 <?php
   // router.php
  if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {
    return false;    // serve the requested resource as-is.
  } else { 
     echo "<p>Welcome to PHP</p>";
  }

Now in Eclipse go to Eclipse->Preferences->PHP->PHP Executables and add a new server: enter image description here

And that should be it. Now create a PHP CLI Run configurations using the wrapper executable as 'Alternate PHP' and for the php file specify the route file:

enter image description here

Then Run as CLI!!! A PHP server should now be listening on port 8000 on your localhost. I suspect this method may also work for HHVM's Proxygen server.

Ray
  • 40,256
  • 21
  • 101
  • 138