2016-09-05 56 views
0

尝试设置yo webapp以使用PHP服务器而不是HTML。使用PHP设置Yeoman Webapp

我试着按照这个答案的例子没有成功。 Gulp-webapp running BrowserSync and PHP

我将gulp-connect-php添加到我的项目中。

// Towards the top of my gulpfile, added: 
const connect = require('gulp-connect-php'); 

// Added the following task below: 

gulp.task('php-serve', ['styles', 'fonts'], function() { 
connect.server({ 
    port: 9001, 
    base: 'app', 
    open: false 
}); 

var proxy = httpProxy.createProxyServer({}); 

browserSync({ 
    notify: false, 
    port : 9000, 

    server: { 
     baseDir : ['.tmp', 'app'], 
     routes : { 
      '/bower_components': 'bower_components' 
     }, 
     middleware: function (req, res, next) { 
      var url = req.url; 

      if (!url.match(/^\/(styles|fonts|bower_components)\//)) { 
       proxy.web(req, res, { target: 'http://127.0.0.1:9001' }); 
      } else { 
       next(); 
      } 
     } 
    } 
}); 

// watch for changes 
gulp.watch([ 
    'app/*.html', 
    'app/*.php', 
    'app/scripts/**/*.js', 
    'app/images/**/*', 
    '.tmp/fonts/**/*' 
]).on('change', reload); 

gulp.watch('app/styles/**/*.scss', ['styles']); 
gulp.watch('app/fonts/**/*', ['fonts']); 
gulp.watch('bower.json', ['wiredep', 'fonts']); 
}); 

gulp php-serve

PHP 5.5.36 Development Server started [etc…] 
Listening on http://127.0.0.1:9001 

然而,并不是没有错误:

ReferenceError: httpProxy is not defined 

这又如何解决?我甚至不介意使用MAMP,我已经在端口8888上运行

回答

2

看来我错过了安装http-proxy的一个重要部分,我认为这是我之前安装的。

这里得到PHP最流行的自耕农发电机,发电机的webapp进行工作,非常感谢TobyG

的白痴指南安装http-proxy

npm install http-proxy --save 

安装gulp-connect-php

npm install --save-dev gulp-connect-php 

将这两个函数添加到的顶部gulpfile.js

const connect = require('gulp-connect-php'); 
const httpProxy = require('http-proxy'); 

这个额外的任务添加到gulpfile.js

gulp.task('php-serve', ['styles', 'fonts'], function() { 
connect.server({ 
    port: 9001, 
    base: 'app', 
    open: false 
}); 

var proxy = httpProxy.createProxyServer({}); 

browserSync({ 
    notify: false, 
    port : 9000, 
    server: { 
     baseDir : ['.tmp', 'app'], 
     routes : { 
      '/bower_components': 'bower_components' 
     }, 
     middleware: function (req, res, next) { 
      var url = req.url; 

      if (!url.match(/^\/(styles|fonts|bower_components)\//)) { 
       proxy.web(req, res, { target: 'http://127.0.0.1:9001' }); 
      } else { 
       next(); 
      } 
     } 
    } 
}); 

// watch for changes 
gulp.watch([ 
    'app/*.html', 
    'app/*.php', 
    'app/scripts/**/*.js', 
    'app/images/**/*', 
    '.tmp/fonts/**/*' 
]).on('change', reload); 

gulp.watch('app/styles/**/*.scss', ['styles']); 
gulp.watch('app/fonts/**/*', ['fonts']); 
gulp.watch('bower.json', ['wiredep', 'fonts']); 
}); 
+0

它的工作原理。我不得不在gulpfile的html任务中改变一些位:从'return gulp.src('app/*。html')'到'return gulp.src(['app/*。html','app/* .php'])',以便将从php文件调用的js和css编译到dist文件夹。 –

+0

此外,您还必须在wiredep任务中执行相同的编辑。 –

+0

如果您已将generator-webapp更新为2.3.2,则需要在建议的代码中更改一行。更改'browserSync({...'到'browserSync.init({'为了避免错误'browserSync不是一个函数...' –