I would like to use Browsersync with PHP, but I can't seem to get it to work properly.
Currently I am using Gulp. Is it possible to either use Browsersync with XAMPP/MAMP, or use a Gulp plugin to read .php
files?
To use Gulp in a PhpStorm project, you need two packages: A globally installed gulp-cli package (Gulp command-line interface) for executing Gulp commands. A gulp package installed as a development dependency to build the project tasks tree and provide coding assistance while editing the Gulpfile. js file.
BrowserSync will automatically monitor your files for changes, and insert your changes into the browser - all without requiring a manual refresh.
How Does BrowserSync Work? BrowserSync starts a small web server. If you're already using a local web server or need to connect to a live website, you can start BrowserSync as a proxy server. It injects small script into every page which communicates with the server via WebSockets.
use gulp-connect-php
npm install --save-dev gulp-connect-php
then setup you gulpfile.js
var gulp = require('gulp'),
connect = require('gulp-connect-php'),
browserSync = require('browser-sync');
gulp.task('connect-sync', function() {
connect.server({}, function (){
browserSync({
proxy: '127.0.0.1:8000'
});
});
gulp.watch('**/*.php').on('change', function () {
browserSync.reload();
});
});
see documentation https://www.npmjs.com/package/gulp-connect-php
Also there is a good tutorial here https://phpocean.com/tutorials/front-end/automate-your-workflow-with-gulp-part-3-live-reloading/23
I have the same situation for a few days until I figure out that it happens because I didn't close my html output properly. BrowserSync needs to include some javascript text before to reload the browser. If the html, body tags are NOT closed or NOT present, BrowserSync has nowhere to include the script.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With