sindresorhus / grunt-php Goto Github PK
View Code? Open in Web Editor NEWStart a PHP server
License: MIT License
Start a PHP server
License: MIT License
I can't make my php-grunt tasks run short-lived , I mean in a non-persistent way.
I am using php-grunt in the context of a project that I want to manage with Yeoman, so my current Grunfile.js is a customization of the Gruntfile.js that Yeoman generators provide.
Despite having set explicitly the keepalive option to false, whenever I run grunt test, the execution of php:test prevents the next task ( mocha ) from being executed.
Everything else seems to be working fine, including the server and PHP5.4 which really gets executed, but keeps running until Ctrl+C is pressed.
I couldn't make it work neither with or without the livereload plugin.
I'd appreciate if you can provide any hint on what the possible causes of this problem can be.
I've spent a great amount of time trying to figure it out how to setup this plugin with livereload so I want to share how I've done it so others don't have a hard time like I did.
/*global module*/
module.exports = function (grunt) {
'use strict';
// config plugins
grunt.initConfig({
php: {
server: {
options: {
hostname: 'localhost',
port: 9000,
open: true,
keepalive: false
}
}
},
watch: {
reload: {
files: ['{,*/}*'],
options: {
// enables a live reload server
// you need to add <script src="//localhost:35729/livereload.js"></script>
// on index.php before the closing </body> tag
livereload: true
}
}
}
});
// load plugins
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-php');
// setup tasks
grunt.registerTask('dev', ['php:server', 'watch:reload']);
};
There is a lot of web apps on my computer that uses a lot of ports and I cannot just pick random port manually for php
because it can be used by other web apps. Also I need to share my project with teammates that have a same situation but with other web apps and busy ports.
So I think that it will be a good to search first available free port and use it instead of picking it manually and have a chance to face busy port.
In grunt-contrib-connect
we can use 0
or ?
value for port
to use system-assigned port [1] but in grunt-php
I have an error with 0
or ?
Invalid address: 0.0.0.0:0
As stated in pull request #34 all 200-, 300- and 400-level HTTP response codes should be accepted as valid. This is not working for HTTP response codes which are treated as an error by http.request(). So if there isn't any index file, server sends a 400 response code and grunt-php retries 21 times (checkServerTries = 20 + one initial try).
I had a deeper look in the problem. It's seems that node js is treating the error response header of php built in webserver as non valid. If you have a look at err
in .on('error', function(err){})
it's set to HPE_INVALID_CONSTANT
.
What's up with this project? Is it still working? Is it abandoned?
Thanks in advanced...
This package relies on sindresorhus/bin-version-check
which in-turn relies on npm/node-semver
. Unfortunately NPM's semver package does not accept the -dev
prerelease suffix used by the developers of PHP. As such the requirement fails validation in the presence of a prerelease build of PHP despite the fact that it meets the specified ``>=5.4` range.
Consider adding text to the README explaining that prerelease versions of PHP are incompatible or look into why the values are not accepted. This may be a bug of this project if the NPM semver component does not accept -dev
intentionally. If this is not the case the issue is somewhere upstream.
I use grunt-php
to serve up development copies of built files to a separate staging site. (The staging site is also PHP-based, but hosted on a separate remote server.) When I'm on the staging site, I'd like to be able to test the status of the grunt-php
server. I've tried PHP's get_headers()
, as well as a few curl
-based solutions to no avail. Was wondering if anyone round these parts might have a hot tip? Thanks in advance!
Hello,
I am getting this error with my php project. I did a few google searches and I found that you have to uncomment this : ;extension=php_pdo_mysql.dll in php.ini
Does grunt-php looked for an installed version of php on my machine. I come from wamp so this is a bit confusing for me.
Thank you !
Edit : found it, needed to use bin option
Hi,
sorry to bother you.
Is there a way to run
php artisan serve
with grunt-php ?
This my gruntfile
'use strict';
var paths = {
js: ['Gruntfile.js', 'public/**/*.js', '!public/bower_components/**'],
html: ['public/**/views/**'],
css: ['public/**/css/*.css', '!public/bower_components/**']
};
module.exports = function(grunt) {
require('time-grunt')(grunt);
// Project Configuration
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
jshint: {
all: {
src: paths.js,
options: {
jshintrc: true
}
}
},
php: {
dist: {
options: {
port: 8000,
keepalive: true,
open: true
}
},
watch: {
options: {
livereload: true
},
page: {
files: ['*.php', '*.html','*.js','*.css'],
tasks: ['php']
}
}
}
});
//Load NPM tasks
require('load-grunt-tasks')(grunt);
grunt.registerTask('default', ['jshint','php']);
};
If grunt-php could accept multiple directories as base, just like grunt-contrib-connect we can easily run a PHP development server using Compass. The files Compass generate during a development cycle aren't copied to the app directory because you like to work from source files. Changing this probably means we can use CoffeeScript more easily as well.
E.g.:
Change
php: {
livereload: {
base: '<%= yeoman.app %>'
}
}
to
php: {
livereload: {
base: ['.tmp', '<%= yeoman.app %>']
}
}
After installing this package, grunt phpwatch
give me this
โฏ grunt phpwatch
Running "php:watch" (php) task
PHP 5.4.17 Development Server started at Tue Dec 17 16:06:15 2013
Listening on http://127.0.0.1:8000
Document root is /Users/MoOx/Sync/Development/XXX
Press Ctrl-C to quit.
[Tue Dec 17 16:06:15 2013] 127.0.0.1:53560 [200]: /
Running "watch" task
Waiting...[Tue Dec 17 16:06:15 2013] 127.0.0.1:53561 [200]: /
Done, without errors.
Not sure what is the issue, just noticing that the server port is not the same in the watch task output...
Here is my watch
grunt.registerTask('phpwatch', ['php:watch', 'watch'])
Noob question I'm sure, but on shared hosting services like BlueHost/GoDaddy/etc. the root directory has a bunch of files .htaccess
, 404 and other error page files, etc. Wondering if I can use these and how? (Or maybe, alternately, if there's any way to use/access/edit a server config file?)
All I want to do is redirect 404 errors to a php template, which can't be that complex as I figure it!
Could we have an example on how to use grunt-php within Yeoman?
If I run the following command from a terminal in the same directory as my GruntFile.js I can load my wordpress site using the built-in server:
php -S 127.0.0.1:35729 -t ../../../.. router.php
However if I run with the following configuration using grunt-php it fails:
php: {
dev: {
options: {
hostname: '127.0.0.1',
port: 35729,
base: '../../../..',
router: 'router.php',
keepalive: false,
open: true,
}
},
},
grunt.registerTask('default', [
'php:dev',
'watch'
]);
The error I get is:
Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0
Fatal error: Unknown: Failed opening required 'router.php' (include_path='.;C:\xampp\php\PEAR') in Unknown on line 0
Is there anything I need to do differently to get this task to work? I'm trying to run a wordpress site and I'm running Windows.
Thanks
This task has been working like a charm on my macbook pro running yosemite, however recently and with little if any change that I can recall on my project I am now getting:
PHP 5.5.14 Development Server started at Mon Nov 10 22:58:51 2014
Listening on http://127.0.0.1:8080
Document root is /Users/micahblu/Projects/micahblu.com
Press Ctrl-C to quit.
[Mon Nov 10 22:58:51 2014] 127.0.0.1:50623 [500]: /
Warning: Server docroot returned 500-level response. Please check your configuration for possible errors. Use --force to continue.
Thanks for any insight on this. Going to run the project up on my ubuntu machine to see if I get it there as well.
Is there any way to configure a .htaccess to work with this?
If I want to run "watch" task with "php", If hostname
is localhost
it doesn't work, it needs to be an IP like on the tests 0.0.0.0
or 127.0.0.1
Workaround
php: {
watch: {
options: {
hostname: "127.0.0.1"
}
}
}
Setting the hostname option to a value other than localhost
causes grunt-contrib-watch to start without livereload enabled.
With grunt-contrib-connect it's possible to use the middleware option to inject e.g. files from outside the base path you can set in grunt-php. Currently that's not possible (see #31), but something like
php: {
watch: {},
test: {
options: {
keepalive: true,
open: true,
base: '<%= config.app %>/',
middleware: function (connect) {
return [
connect.static('.tmp'),
connect().use('/bower_components', connect.static('./bower_components')),
connect.static(config.app)
];
}
}
}
}
would be awesome to use grunt-php and grunt-wiredep. :)
HI, I use grunt-php in my project to run a php server, but I don't know how to set a variables in *.php? I use to get a value in php , so how can i set it?
I just started using this plugin, and it's looking really solid so far; good job!
Any thoughts on a livereload feature in the future?
Is there anyway to process .html files as php? We use .html extensions on our files but would normally use apache to run them as php.
I want to open the browser of my choice rather than the default system browser when the server starts running. In grunt-contrib-connect
I am able to do this:
connect: {
dev: {
options: {
keepalive: false,
port: '<%= project.port %>',
hostname: '<%= project.hostname %>',
open: {
target: 'http://<%= project.virtualHost %>:<%= project.port %>/<%= project.url %>',
appName: '<%= project.browser %>'
}
}
}
},
Currently my default browser is Safari and the server keeps opening that browser - but I want to launch Google Chrome when server starts instead.
I'm pretty new to Grunt, but I can't seem to get grunt-php to work (has worked fine with Yeoman). I followed examples on grunt-php's readme.md, but whenever I run 'grunt php' or 'grunt test' I get the following error: Fatal error: spawn ENOENT
Here's my gruntfile:
'use strict';
module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-php');
grunt.loadNpmTasks('grunt-mocha');
grunt.initConfig({
mocha: {
all: {
options: {
run: true,
urls: ['http://localhost:<%= connect.options.port %>/index.html']
}
}
},
php : {
dist: {
options: {
port: 5000,
base: 'subfolder'
}
},
test: {
options: {
keepalive: true,
open: true
}
}
}
});
grunt.registerTask('test', [
'php',
'mocha'
]);
grunt.registerTask('default', [
'php'
]);
};
I'm running this on Windows 7 if that matters.
Hi guys,
I testing this nice grunt module, but when i start "grunt task", i've this message :
Warning: Couldn't find the `php` binary. Make sure it's installed and in your $PATH Use --force to continue.
All dependency are installed, maybe i need php on my PC ? Can you help me ?
Thank you :)
Hello,
first of all thank you for this grunt plugin! I've a (noob?) question related to my minimal grunt config:
module.exports = function(grunt) {
'use strict';
grunt.initConfig({
php: {
default: {
options: {
port: 9000,
base: 'app/',
open: true,
bin: '/opt/local/bin/php55'
}
}
}
});
grunt.loadNpmTasks('grunt-php');
grunt.registerTask('default', [
'php'
]);
};
every time I call grunt
from command line I get the following output, that seems to me like something is continuously looking for a "/" page on progressively increasing local ports, and the server returns a HTTP 500 error (no process is serving pages on that ports). But I do not get why this happens, the php server works perfectly in the meantime, and if I run php -S localhost:9000
directly from the command line I do not get these messages.
$ grunt
Running "php:default" (php) task
PHP 5.5.7 Development Server started at Sat Dec 28 11:37:30 2013
Listening on http://127.0.0.1:9000
Document root is /Users/stefano/sviluppo/siti/noon/app
Press Ctrl-C to quit.
[Sat Dec 28 11:37:30 2013] 127.0.0.1:53757 [500]: /
[Sat Dec 28 11:37:30 2013] 127.0.0.1:53758 [500]: /
[Sat Dec 28 11:37:30 2013] 127.0.0.1:53759 [500]: /
[Sat Dec 28 11:37:30 2013] 127.0.0.1:53760 [500]: /
[Sat Dec 28 11:37:30 2013] 127.0.0.1:53761 [500]: /
[Sat Dec 28 11:37:30 2013] 127.0.0.1:53762 [500]: /
[Sat Dec 28 11:37:30 2013] 127.0.0.1:53763 [500]: /
[Sat Dec 28 11:37:30 2013] 127.0.0.1:53764 [500]: /
[Sat Dec 28 11:37:30 2013] 127.0.0.1:53765 [500]: /
[Sat Dec 28 11:37:30 2013] 127.0.0.1:53766 [500]: /
[Sat Dec 28 11:37:30 2013] 127.0.0.1:53767 [500]: /
[Sat Dec 28 11:37:30 2013] 127.0.0.1:53768 [500]: /
[Sat Dec 28 11:37:30 2013] 127.0.0.1:53769 [500]: /
[Sat Dec 28 11:37:31 2013] 127.0.0.1:53770 [500]: /
[...]
I do not even know if this is strictly related to grunt-php
but the log is annoying and somehow prevents me to read eventual messages from the console (they scroll up rapidly).
I'm on a mac, OS X 10.9.1 (but I've the same issue with 10.8.x).
thanks in advance for any suggestion on how to suppress these messages!
Hi!
I'm using yeoman with angular and I want using php on my project.
I installed grunt-php and after that, when I run "grunt php" It says " No "php" targets found. "
I've just started running grunt-php. I had an issue setting up grunt-php with watch. It's probably worth extending the ReadMe example, specifically to say that when you are using "watch", you cannot use "keepalive: true" in the php task.
I'm also having horrible performance serving all files, compared to when I was using PHP through iis-express. Any thoughts on what to look for?
I'm running this task on a project that runs the UI with grunt-contrib-connect in the 8080 port, and grunt-php in the 9090 port for an API.
In Mac and Linux, it just works fine, it runs the php:server task and the continue with the watch task.
But in windows, when it runs the php:server task it blocks the next task to be executed, and then the watch task never get executed.
I'm not really sure if this is an issue of PHP by itself or related this plugin. Both, Unix and Windows are running php 5.5
re #42. would be nice to document as it's not very succinct.
Hi the commits that fixed #70 created a bug for me.
When I put '../' in base. The php server wont start and sais Directory does not exit.
'..' does not work either.
In order to set project specific configuration options, using a custom php.ini file in your project would add the ability to set extra options. This is especially helpful for settings that cannot be set at runtime (ini_set()
).
Adding -c path/to/php.ini
argument to php.js
would be worth exploring?
I noticed that my grunt serve task worked and fired up my browser but that then going to a php file it downloaded it. I am thinking why this was and ended up here in your project looking for answers.
I am trying to proxy in an API onto my test dev machine so need the php script to solve a cross domain issue.
So I need to go through installing this if I want to get php working correct..at least as far as the quick test server that gets fired up from grunt serve.
Thanks for your advice / answer help etc
I completely forgot about it when creating this task, but might be better than firing up the built-in PHP server.
https://github.com/fgnass/gateway
@fgnass what are the differences between using Gateway and the built-in PHP server? Are there any downsides?
Im using right now a simple integration with watch task from grunt-contrib-watch and it works but is very slow when reload.
In details I have my grunt php configured like this:
php: {
port: 8000,
hostname: 'localhost',
watch: {}
},
and my watch task are configured like this:
watch: {
sass: {
files: ['**/*.scss'],
tasks: ['compile-sass']
},
php: {
files: ['**/*.php'],
},
js: {
files: ['**/*.js'],
},
html: {
files: ['**/*.html', '**/*.hbs'],
},
options: {
nospawn: true,
livereload: true,
events: ['all'],
}
},
And finally on my index.php I included the livereload.js script tag.
Is this normal or I'm doing something wrong?
On my windows 7 machine i am getting the following error message:
Class 'mysqli' not found in....
(- it's working fine on my mac)
can the *.php files refresh in the browser automatically just like the function 'livereload' run in 'grunt-contrib-connect' when you change some code?
I faced with the error when I was trying to use grunt-php via load-grunt-tasks.
Running "php:dist" (php) task
Warning: Cannot call method 'apply' of undefined Use --force to continue.
It seems to me, that lack of description in the task's method definition should be the reason of that behavior
Hey,
When I run the script I get a stream of [200] errors in my terminal window. Here's the output I'm getting:
[Sat Apr 12 23:05:40 2014] 127.0.0.1:51206 [200]: /assets/js/main.js
[Sat Apr 12 23:05:40 2014] 127.0.0.1:51207 [200]: /assets/js/plugins.js
[Sat Apr 12 23:05:40 2014] 127.0.0.1:51208 [200]: /assets/json/listing.json
[Sat Apr 12 23:05:49 2014] 127.0.0.1:51210 [200]: /assets/php/save.php
[Sat Apr 12 23:05:50 2014] 127.0.0.1:51212 [200]: /
Here's my config:
php: {
dev: {
options: {
hostname: '0.0.0.0',
open: true
}
}
},
Any idea why this might be happening?
Is there a way to suppress messages generated in the shell for every resource that is loaded? This gets in the way of other messages happening while grunting. Like Sass processing messages, linting output, etc.
As of the latest PHP update (7.0.8), this error is thrown right on startup.
System: Ubuntu 16.04 LTS
Gruntfile.js:
"use strict";
module.exports = function(grunt) {
require('load-grunt-tasks')(grunt); // npm install --save-dev load-grunt-tasks
grunt.initConfig({
php: {
test: {
options: {
keepalive: true,
open: false // We don't have to open the back-end in the browser...
}
}
}
});
grunt.registerTask('serve', ['php']);
};
Package.json:
{
"name": "api",
"version": "1.0.0",
"description": "API Backend",
"main": "gruntfile.js",
"author": "Arjan Vlek",
"devDependencies": {
"grunt": "^1.0.0",
"grunt-php": "^1.5.1",
"load-grunt-tasks": "^3.5.0"
}
}
Php -v:
PHP 7.0.8-0ubuntu0.16.04.2 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.8-0ubuntu0.16.04.2, Copyright (c) 1999-2016, by Zend Technologies
The task should fail with an error if PHP isn't installed or the version isn't valid (<5.4.0).
This is not really an issue, but more a request for a tiny enhancement.
When there's no php-file in the root of the project grunt-php keeps on giving the following error in the console: "127.0.0.1:45145 [404]: / - No such file or directory", several times a second.
Grunt-php however runs normally. I fixed this by putting an empty index.php in the root of my project.
How would you run this along side compass/sass? Trying to view a php web server with the SCSS/CSS changes being watched as well, not too sure if I'm doing something stupid. Gruntfile.js below:
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
compass: {
options: {
config: 'config.rb',
}
},
php: {
options: {
port: 8000,
keepalive: true,
open: true,
base: 'public_html/',
hostname: 'localhost'
},
watch: {
options: {
livereload: 8000
},
scss: {
files: '**/*.scss',
tasks: ['compass'],
options: {
livereload: false
}
},
css: {
files: '**/*.css',
tasks: [],
livereload: 8000
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-compass');
grunt.loadNpmTasks('grunt-php');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('phpwatch', ['php:watch', 'watch']);
}
Add environment variables can be of great help. ๐
Hey Sindre,
I'm just taking a look at this grunt-php plugin. I believe its exactly what I'm looking for, however, I can't seem to get it to work. Likely something to do with the fact that I'm a grunt newb.
I tried a number of Gruntfile configurations and I'm just trying to get it to work in the default yeoman "webapp" by switching it out for grunt-contrib-connect.
Can you point me in the right direction? I'm all grunt, yeoman and getstarted read out and need a bit of a push.
Cheers,
Dan
OSX 10.7.5
Node v0.8.9
grunt-cli v0.1.7
grunt v0.4.1
Hi,
I am testing the module grunt-php in local using a Windows machine but I receive the following message:
"Running "php:dist" (php) task
Warning: Couldn't find the `php` binary. Make sure it's installed and in your $P
ATH Use --force to continue.
Aborted due to warnings."
What is the problem?
module.exports = function (grunt) {
'use strict';
grunt.initConfig({
php: {
dist: {
options: {
hostname: '127.0.0.1',
port: 9000,
base: './FOLDER_WITH_PHP_FILES', // Project root
keepalive: false,
open: false
}
}
}
});
grunt.loadNpmTasks('grunt-php');
grunt.registerTask('serve', [
'php:dist', // Start PHP Server
]);
};
Had a look at this question tonight and it doesn't seem like grunt-php
calls the open() method when the option is set to true, at least on my machine and the asker's. I'm running Mavericks. Was just wondering if anyone else can replicate this?
https://github.com/sindresorhus/grunt-php/blob/master/tasks/php.js#L51-L59
A temporary fix could be as simple as the one I offered in the question.
checkServer('http://' + host, function () {
if (!this.flags.keepalive && !options.keepalive) {
cb();
}
}.bind(this));
// open the browser straight away
if (options.open) {
open('http://' + host);
}
Thanks.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.