使用PHP设置Yeoman Webapp

时间:2016-09-05 14:54:49

标签: php generator yeoman browser-sync

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

我尝试通过示例关注此答案但没有成功。 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

如何解决这个问题?我甚至不介意使用已在端口8888上运行的MAMP

1 个答案:

答案 0 :(得分:3)

似乎我错过了安装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']);
});