Gulp-webapp running BrowserSync and PHP - php

My main goal here is to adapt Yeoman's gulp-webapp development workflow to run PHP.
Specifically, I want to be able to use gulp-php-connect with multiple base directories (for the compiled CSS from Sass) and routes (for Bower dependencies), if that's even possible.
I'm able to run PHP with Gulp using the gulp-connect-php plugin, like this:
gulp.task('connect-php', function() {
connectPHP.server({
hostname: '0.0.0.0',
bin: '/Applications/MAMP/bin/php/php5.5.3/bin/php',
ini: '/Applications/MAMP/bin/php/php5.5.3/conf/php.ini',
port: 8000,
base: 'dev'
});
});
However, I'd like to take advantage of gulp-webapp's excellent but quite entangled development workflow architecture, which relies on BrowserSync, Sass compiler (compiles to a .css file into a .tmp folder, for development), auto-prefixer, and uses a bunch of other useful plugins.
Here's the part of it that I would like to adapt to use gulp-connect-php or any other PHP :
gulp.task('serve', ['styles'],function () {
browserSync({
notify: false,
port: 9000,
server: {
baseDir: ['.tmp', 'app'],
routes: {
'/bower_components': 'bower_components'
}
}
});
// watch for changes
gulp.watch([
'app/*.html',
'.tmp/styles/**/*.css',
'app/scripts/**/*.js',
'app/images/**/*'
]).on('change', reload);
gulp.watch('app/styles/**/*.scss', ['styles', reload]);
gulp.watch('bower.json', ['wiredep', 'fonts', reload]);
});
BrowserSync has a proxy option, that allows me to run it with gulp-connect-php server, which is pretty amazing. But I need gulp-connect-php it to use multiple base directories and routes, like BrowserSync does.
So far I've come up with this:
gulp.task('serve-php', ['styles','connect-php'],function () {
browserSync({
proxy: "localhost:8000"
});
// watch for changes
gulp.watch([
'app/*.php',
'app/styles/**/*.css',
'app/scripts/**/*.js',
'app/images/**/*'
]).on('change', reload);
gulp.watch('app/styles/**/*.scss', ['styles, reload]);
gulp.watch('bower.json', ['wiredep', 'fonts', reload]);
});
To temporarily fix the multiple base directories issue, I tweaked the styles task so it stores the compiled .css to /app instead of .tmp/. I'd prefer to have it on a temp folder though, because I don't need that compiled .css file hanging around there with my Sass files.
For the routes issue, I'm trying to tell wiredep plugin to change a path, say, from bower_components/jquery/dist/jquery.js to ../bower_components/jquery/dist/jquery.js, with no success.
All I could do was manually rename the paths in index.php, and it still doesn't work. When running gulp serve I get:
/bower_components/jquery/dist/modernizr.js - No such file or directory
...even though I changed the path in index.html to ../bower_components/jquery/dist/jquery.js.
I believe that doesn't work because the gulp-connect-php server can't see what's outside the base folder.
I'm trying different things, and though I've been pretty vague on this thread's title, I think that the cleanest solution would be to run multiple base directories and routes with gulp-connect-php, but I don't know if that's possible.

I spent a while trying to work this one out, but have a working solution now. The way I solved is was to use BrowserSync as the server, and added a middleware that proxies requests if they don't match a pattern...
Install the http-proxy package...
$ npm install --save-dev http-proxy
Add the proxy package to the top of the gulpfile.js...
var httpProxy = require('http-proxy');
Add a separate php server and a proxy server before the BrowserSync...
gulp.task('php-serve', ['styles', 'fonts'], function () {
connect.server({
port: 9001,
base: 'app',
open: false
});
var proxy = httpProxy.createProxyServer({});
// ...
Then add the middleware for the server to see if the request needs to be proxied...
// ...
server: {
baseDir : ['.tmp', 'app'],
routes : {
'/bower_components': 'bower_components'
},
// THIS IS THE ADDED MIDDLEWARE
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();
}
}
}
// ...
And here's the full tasks for completeness...
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']);
});
Hope this saves you all the time I spent working this out! :o)

FWIW, I've got a quite simple and fair solution for that by placing the compiled .css file in the app/ root and moving /bower_dependencies folder inside the app/ folder.
For Sass, I only needed to change the path in the placeholder to <!-- build:css styles/main.css --> and change the dest in the styles task.
For the bower_components, I just edited bower_components in .bowerrc:
{
"directory": "app/bower_components"
}
and added this to the wiredep stream in gulpfile.js:
fileTypes: {
scss: {
replace: {
scss: '#import "app/{{filePath}}";'
}
}
},

Related

PHP with Vue CLI how to set vue.config.js

Trying to Use PHP with Vue CLI, I'm trying to replicate this :
https://forum.vuejs.org/t/using-php-with-vue-cli/52842/3
but failed.
I have this folder structure:
myapp/
public/
src/
vue.config.js
back/
index.php
When I run npm run serve from myapp/, I go to http://localhost:8080 and see my app.
In an other folder called 'back', I have my php file(s).
I run a server with mamp and I access this folder at this address : http://back:8888
In my vue.config.js I have
module.exports = {
devServer: {
proxy: {
'^/api': {
target: 'http://back:8888',
changeOrigin: true,
logLevel: "debug"
}
}
}
};
From what I understood, I should have the same page when I go to http://localhost:8080/api and http://back:8888.
But I don't, I get a 404 for http://localhost:8080/api, and back:8888 is fine.
In the terminal I have this [HPM] GET /api -> http://back:8888
So it does detect a redirection.
The weirdest thing is that with this :
module.exports = {
devServer: {
proxy: {
'^/api': {
target: 'http://back:8888/index.php',
changeOrigin: true,
logLevel: "debug"
}
}
}
};
when I visit localhost:8080/api, I do have the output of the index.php in my back folder.
What am I missing here ?
Thanks
I add miss read the https://forum.vuejs.org/t/using-php-with-vue-cli/52842/3.
localhost:8080/api == http://back:8888/api, and NOT like I though :
localhost:8080/api == http://back:8888.

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?
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
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.
A proxy option is now available in browser-sync when using an external server.
Config docs : https://browsersync.io/docs/options#option-proxy
// Using a vhost-based url
proxy: "local.dev"
// Using a localhost address with a port
proxy: "localhost:8888"
// Using localhost sub directories
proxy: "localhost/site1"
Command line : https://browsersync.io/docs/command-line#start
browser-sync start --proxy "localhost:8080" --files "**/*"

Endless reload with browserSync and gulp-connect-php

I finally got around to build my own task runner but I'm stuck on a feature I really need, namely browserSync but I use php with XAMPP.
The problem: terminal keeps telling me "PHP server not started. Retrying...".
Since I'm new to all this I can't seem to figure out why the server's not connecting. Please help?
Here is my code:
// Required tasks
var gulp = require('gulp'),
connect = require('gulp-connect-php'),
browserSync = require('browser-sync'),
reload = browserSync.reload;
// Php Server Tasks
gulp.task('connect', function() {
connect.server({
base: './',
port: 8010,
keepalive: true
});
});
// Browser-Sync Tasks
gulp.task('browser-sync',['connect'], function() {
browserSync({
proxy: '127.0.0.1:8010',
port: 8080,
open: true,
notify: false
});
});
// Watch Tasks
gulp.task ('watch', function(){
gulp.watch('src/sass/**/*.scss', ['styles']);
gulp.watch('src/js/**/*.js', ['scripts']);
gulp.watch('./templates/**/*.php', ['html']);
});
// Default
gulp.task('default', ['browser-sync', 'watch']);
Can't remember where this solution popup but thought I'd share the code as it just may help someone else:
To be clear: I no longer make use of gulp-connect-php.
// BROWSER-SYNC TASKS
gulp.task('browser-sync', function() {
browserSync({
proxy: 'localhost/path/to/the/files',
open: true,
notify: false
});
});

Gulp PHP with BrowserSync and a proxy server throwing error

I had to redownload all of my dev tools after upgrading/reset on Windows 10. I can't seem to get my server working with gulp-connect-php, gulp-browser-sync, and http-proxy. The task (below) is actually from this answer: Gulp-webapp running BrowserSync and PHP.
This is on top of a generation of gulp-webapp from Yeoman. The regular "serve" works, but of course doesn't spin up PHP files. I had it working before the upgrade, but now the http-proxy errors out with an ECONNREFUSED when I try to bring up the URL. Note that it doesn't error out until I actually go to the URL, if the task is setup to not open the browser automatically, it won't error until I go to localhost:3000.
Any ideas the cause of this error?
Gulp Task
gulp.task('php-serve', ['styles', 'fonts'], () => {
phpConnect.server({
port: 9001,
base: 'app',
open: false
});
const proxy = httpProxy.createProxyServer({});
browserSync({
notify: false,
open: 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();
}
}
}
});
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 Run Task with Error
$ gulp php-serve
[07:30:09] Requiring external module babel-core/register
[07:30:11] Using gulpfile ~\...\gulpfile.babel.js
[07:30:11] Starting 'styles'...
[07:30:12] Starting 'fonts'...
[07:30:12] Finished 'styles' after 1.08 s
[07:30:12] Finished 'fonts' after 306 ms
[07:30:12] Starting 'php-serve'...
[07:30:12] Finished 'php-serve' after 183 ms
[BS] Access URLs:
----------------------------------
Local: http://localhost:9000
External: http://x.x.x.x:9000
----------------------------------
UI: http://localhost:3001
UI External: http://x.x.x.x:3001
----------------------------------
[BS] Serving files from: .tmp
[BS] Serving files from: app
C:\...\node_modules\http-proxy\lib\http-proxy\index.js:119
throw err;
^
Error: connect ECONNREFUSED
at exports._errnoException (util.js:746:11)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19)
I think I know the reason.
Check or you have php installed on your machine and added to your path variables.
open your command line and type:
$ php --version
If it returns:
sh.exe": php: command not found
It means it is not installed or not in the path variable.
You can install php by simply installing WAMP/XAMP or MAMP
https://www.apachefriends.org/index.html
To make sure it is in your path I like to use patheditor
https://patheditor2.codeplex.com/
There go through these simple steps
1) open Path Editor
2) click 'add' in the 'user' section
3) search the folder where php.exe is. (usually something like: C:/xampp/php)
4) click 'ok' and 'ok' again to close path editor
5) reopen your commandline tool and you will see that $ php --version will find the php.exe
Try to run your gulp task again. It should work!
There was a bad install of http-proxy and/or gulp-php-connect. I uninstalled them, cleared npm cache, and did a fresh install. Everything worked fine after that.

gulp connect-php only looking in root

I've got a gulp project where I'm using some PHP to pull in patterns to make a styleguide. I've got gulp-connect-php installed and working but it's only running in the ROOT folder and I need it to be looking at whatever is in DIST/
I've got this code here so it uses BrowserSync too
gulp.task('connect-sync', function() {
connect.server({}, function(){
browserSync({
proxy: 'localhost:8000'
});
});
})
I've tired dir and baseDir but neither work.
The full gulpfile.js is here
https://gist.github.com/sturobson/9a616c2cbebfc5059bf3
any help would be greatly appreciated.
Is it perhaps the "files" you are looking for:
http://www.browsersync.io/docs/options/#option-files
example:
files: [
'public/**/*',
'resources/views/**/*'
]

Categories