Skip to content

Example usage of gateway module to serve PHP files from Yeoman

Notifications You must be signed in to change notification settings

falsetto/php-from-yeoman

Repository files navigation

(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.

Generate an app with Yeoman

$ mkdir php-from-yeoman
$ cd php-from-yeoman
$ yo webapp

Ensure that php-cgi is in your $PATH

$ 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

Setup gateway module

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

Rename app/index.html and test

$ 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.

Note: due a to naïve conditional in the livereload library, live reloading will only work for PHP files when the URL ends with a '/' (so basically, index.php files).

About

Example usage of gateway module to serve PHP files from Yeoman

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published