Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use browser sync with php

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?

like image 239
dr_fluff Avatar asked Mar 19 '17 21:03

dr_fluff


People also ask

Does gulp work with PHP?

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.

What is browser sync client?

BrowserSync will automatically monitor your files for changes, and insert your changes into the browser - all without requiring a manual refresh.

How does Browsersync proxy work?

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.


2 Answers

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

like image 195
Ivan of uganda Avatar answered Oct 07 '22 01:10

Ivan of uganda


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.

like image 24
Yeah.not Avatar answered Oct 06 '22 23:10

Yeah.not