为PHP服务器和浏览器同步配置gulpfile.js

时间:2017-04-30 14:16:41

标签: php server gulp localhost browser-sync

我想在本地创建一个PHP服务器,通过Browser Sync监视我文件的更改。我希望它在我的浏览器中打开新选项卡并显示index.php文件的内容。我的gulpfile.js:

var gulp = require('gulp'); 
var browserSync = require('browser-sync').create(); //reloading while saving file
var php = require('gulp-connect-php'); //for PHP server

// ------------------------------------------------- //
// --------- Server and watching for changes --------//
// ------------------------------------------------- //


gulp.task('php', function() {
    php.server({ base: 'src', port: 8080, keepalive: true, hostname: 'localhost'});
});

gulp.task('browser-sync',['php'], function() {
    browserSync.init({
        proxy: '127.0.0.1',
        port: 8080
    });
});

gulp.task('htmlServer', function() {
   gulp.src(['src/*.+(html|ico|php)'])
   .pipe(browserSync.reload({
      stream: true
   }));;
});

gulp.task('stylesServer', function() {
   gulp.src(['src/styles/*.css'])
   .pipe(browserSync.reload({
      stream: true
   }));
});

gulp.task('jsServer', function() {
    gulp.src(['src/scripts/*.js'])
    .pipe(browserSync.reload({
      stream: true
   }));
});

// --------- wrapped together --------- //

gulp.task('server', ['browser-sync','php','htmlServer','stylesServer','jsServer'], function() {
    gulp.watch('src/styles/*.css',['stylesServer']);
    gulp.watch('src/*.+(html|php)',['htmlServer']);
    gulp.watch('src/scripts/*.js',['jsServer']);
});

但是,当我启动gulp服务器任务时,它会在浏览器中弹出新选项卡并继续像在屏幕上一样加载:

enter image description here

有人能帮助我吗?我正在寻找许多教程,但没有任何工作,我不想越来越困惑。预先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

var gulp = require('gulp'); 
var browserSync = require('browser-sync').create(); //reloading while saving file
var php = require('gulp-connect-php'); //for PHP server

检查这部分你写了gulp但是你需要写gulp.php并且在你调用任何文件名的地方你必须添加文件的扩展名然后你在你的项目中调用这个文件