(from Coderwall proptip published at https://coderwall.com/p/bal2_a)
Ever needed to use a PHP script in your Yeoman project? It's quite straightforward to do so while keeping all of the goodness that Yeoman brings together for you (particularly live reloading).
The integration is accomplished via a middleware by Felix Gnass named gateway. Gateway enables you to specify CGI handlers for requests matching certain extensions (.php in our case). Mr. Gnass wrote a blog post on this protip's topic, but didn't include step-by-step instructions for the middleware integration, which took me a while to figure out.
$ mkdir php-from-yeoman
$ cd php-from-yeoman
$ yo webapp
$ which php-cgi
If you get php-cgi not found
, here's how to install it using Homebrew on OS X (for other OS's, Google is your friend):
brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php
brew install php54
$ npm install gateway
Modify Gruntfile.js
by adding the following near the top:
var gateway = require('gateway');
Modify the array returned by the function at connect.livereload.options.middleware
to look like this:
middleware: function (connect) {
return [
lrSnippet,
gateway(__dirname + '/app', {
'.php': 'php-cgi'
}),
mountFolder(connect, '.tmp'),
mountFolder(connect, 'app')
];
}
$ mv app/index.html app/index.php
Add <?php echo "<h1>Hello from PHP!</h1>";
within the body
tag of app/index.php
.
Then fire up your development server:
$ grunt server
Assuming that your browser launched and load http://localhost:9000/
, you should see Hello from PHP! on the page.
Make some changes to app/index.php, save the file, and you should your browser should automatically reload.