PHP Browser Sync
Right off the bat, I am not a proficient PHP developer. Coming from a node js development, there are many tools and library available at our disposal to streamline our development speed. One of the most convinient is the auto refresh for whatever web development I am building on.
I am currently building a Php backend to leverage on my readily available from my web-hosting. Not wanting to spend more on another new server and machine. I decided to give PHP a try, and try to build something that can store additional info into my database on top of the MS-Graph interfaced application for my simple accounting software.
This is where Browser Sync to the rescue. As long as you have npm available in your machine. Global install the library will do.
npm install -g browser-sync
Then here is a bunch of command line fromt he website to use.
# Examples # Start a server from the `app` directory, watching all files $ browser-sync start --server 'app' --files 'app' # As above, but with version >= 2.23.0 $ browser-sync app -w # Start a server from the `app` & `.tmp` directories (short hand) # (requires 2.12.1) $ browser-sync start -s 'app' '.tmp' -f 'app' '.tmp' # As above, but with version >= 2.23.0 $ browser-sync 'app' '.tmp' -w # Proxy a PHP app + serve static files & watch them $ browser-sync start --proxy 'mylocal.dev' --serveStatic 'public' --files 'public' # As above, but with version >= 2.23.0 $ browser-sync 'http://mylocal.dev' 'public' -w # Start Browsersync from a config file $ browser-sync start --config 'conf/browser-sync.js' # Start Browsersync from a config file with overriding flags $ browser-sync start --config 'conf/browser-sync.js' --port 4000
For my case, a slight modification to the project directory. I am ready.
browser-sync start --proxy localhost --files .
But wait….
The sync is not working. As per in this issue. Browser Sync basically requires an attachment of some javascript to attach for the reload.
Work around
I have added a tag before and after my JSON return.
echo "<body><pre>"; // my logic echo "</pre></body>"
For now, this work for me to speed up my learning to program and see output instantly.
Note* I am currently using UniformServer as this is the lightest PHP server for me to build locally before putting it into the web-hosting.