2015-11-16 36 views
3

我目前使用的是Yeoman Generator Gulp-Webapp,我稍微修改它以使其適用於PHP。我只需添加gulp-connect-php & http-proxy,然後通過添加以下代碼來編輯gulpfile.babel.js browserSync任務。現在我需要找到一種方法,使其與htaccess工作。任何想法如何做到這一點?瀏覽器同步 - PHP和htaccess?

gulp.task('serve-php', ['styles', 'fonts'],() => { 
    phpConnect.server({ 
    port: 9001, 
    base: 'app', 
    open: false 
    }); 
    const proxy = httpProxy.createProxyServer({}); 
    browserSync({ 
    notify: false, 
    open: true, 
    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(); 
     } 
     } 
    } 
    }); 

    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

你被Apache得到了什麼樣的錯誤? –

+0

htaccess文件根本沒有讀取,所以我沒有得到任何錯誤。事實上,它不是apache服務器,而是node.js的'虛擬'服務器。 –

回答

1

我能夠使它與PHP和htaccess一起工作!

我沒有使用gulp-connect-php來創建虛擬主機,而是使用了XAMPP。然後,我將代理目標指向XAMPP虛擬主機。以下是我做的:

gulp.task('serve-php', ['styles', 'fonts'],() => { 
    const proxy = httpProxy.createProxyServer({}); 
    browserSync({ 
    notify: false, 
    open: true, 
    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://xamppvhost.dev' }); 
     } 
     else { 
      next(); 
     } 
     } 
    } 
    }); 

    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']); 
}); 

我刪除phpConnect.server(),並改變了proxy.web()來定位我的XAMPP的虛擬主機。

現在一切正常!

0

也許這對於那些碰到這個的人來說仍然有用。

如果你在Mac開發時,可以使用甲基苯丙胺和一飲而盡,甲基苯丙胺,目前最簡單的方法把事情的工作 - PHP,htaccess的和MySQL如果需要的話。

https://www.npmjs.com/package/gulp-mamp

安裝後,你可以得到的東西只此代碼在您的工作gulpfile:

var mamp = require('gulp-mamp'); 
gulp.task('mamp-start', function(cb){ 
    mamp(options, 'start', cb); 
}); 

,然後添加任務爲默認任務,或通過命令gulp mamp-start

毫安+瀏覽器同步

也很容易,只要將代碼從這個gulpfile複製:

https://github.com/sdotson/gulp-mamp-browsersync/blob/master/gulpfile.js