Pow with Laravel

In the past1, I’ve been a big fan of running a virtualized web server locally instead of intalling PHP and Apache (or Nginx) directly on my development machine. I think I just changed my mind.

A little over a month ago, I read Elliot Jay Stocks’ article abou the stuff he missed on vacation. One of those things was the release of Anvil. Anvil is really just a nice looking front-end for Pow. Pow is a local rack web server built on node.js. It adds some cool stuff to /etc/resolvr to handle DNS for development domains and then you just symlink a directory into ~/.pow to get a server at a domain like .dev.

For some reason I’ve never really been a fan of MAMP. I think the “zero-config” thing stuck with me when I saw Pow though. So I started looking around2 to see if I could use Pow with PHP apps.

It turns out to be possible. There’s a gem called rack-legacy which is exactly for this purpose. Unfortunately rack-legacy uses the CGI version of PHP which doesn’t come in Mountain Lion. So you have to install PHP from source, which requires you to install mcrypt from source as well.

  1. Install mcrypt from source.
  2. Download the PHP source over at the PHP Downloads page.
  3. cd into the PHP source directory and configure it ./configure --with-cgi --enable-mbstring --with-curl --with-xmlrpc --with-mcrypt=/usr
  4. make and make install PHP.

So, now that we have PHP set up, install Pow, Rack, and all the other dependancies.

brew install node sqlite
gem install rack rack-legacy rack-rewrite
curl get.pow.cx | sh

I think the only dependancy for pow is node, which I used homebrew to install. I also installed sqlite because it’s easier to use locoally than MySQL. And Laravel makes it simple to switch over to a SQLite database.

It’s finally time to set up our Laravel app. Rack apps need a config.ru file in the root which is just a Ruby script that configures the server for this particular app. Put the following config.ru in Laravel’s public folder and point Pow at the same folder.

require 'rack'
require 'rack-legacy'
require 'rack-rewrite'

use Rack::Rewrite do
send_file %r{([^?]+)}, Dir.getwd + '$1', :if => Proc.new { |env|
path = File.expand_path(Dir.getwd + env['PATH_INFO'])
File.file?(path)
}
rewrite %r{(.*)?}, 'index.php$1'
end

use Rack::Legacy::Php, Dir.getwd
run Rack::File.new Dir.getwd

That’s it.

Resources

There were kind of two main resources that I used to fully understand what was going on here and how to set this up. If you’re going to be a PHP developer and use Ruby tools, get used to seeing PHP as “Legacy Development” I guess.

  1. Legacy Development with Pow
  2. Configuration file to use PHP with rack

  1. I’ve written a few articles about setting up a VM in VirtualBox for PHP development locally. Including the original, part 2, the one about nginx, and the video

  2. I even considered writing my own “zero-configuration” web server that would work with PHP for a minute. 

Ethiopian kids hack OLPCs in 5 months

With no instruction, kids in Ethiopia who had never seen written words managed to hack Android in 5 months. It’s cool to see news like this once in a while to remind us what an awesome project OLPC is.

Post Format Loops in WordPress

Post formats in WordPress are pretty cool. According to the Codex, “A Post Format is a piece of meta information that can be used by a theme to customize its presentation of a post.” In my mind post formats are to present content in a way that makes sense for a given format. For example, video posts should highlight the video. Or maybe link posts should link directly to the site you want your readers to see instead of the permalink for that article.

Here I’m going to take a look at creating different templates for specific formats.

First, I’ll say that a good way to get started with post formats is just to enable the ones you want with the appropriate call to add_theme_support()1 in your theme’s functions.php file and customize them with CSS. If your theme takes advantage of the post_class() function to add classes to posts, each post will get a class in a style like “format-$format”, where $format is the name of the format — “format-gallery”, for example.

If you decide that you need to take it a step further and actually generate different HTML depending on the format, the following is a technique that I’ve used. Let’s start by looking at the loop in index.php of the _s theme, available on Github.

<!--?php /* Start the Loop */ ?-->
<!--?php while ( have_posts() ) : the_post(); ?-->

<!--?php <br ?--> /* Include the Post-Format-specific template for the content.
* If you want to overload this in a child theme then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
?>

 

The most interesting part of this is get_template_part( 'content, get_post_format() ), which will look for a file with a name like “content-$format.php”. If no such file exists, it will fall back to “content.php”, which will be our default for post types that we’re not going to specify special templates for.

If we think ahead a little bit we’ll realize that all of our posts will probably share some common structure. In my case, I want the title to be displayed if one was specified, certain meta information should be displayed, and of course I want to display the content. So I’m going to create a file called “content-base.php” with that basic structure. Then in content.php, I’ll basically just make a call to get_template_part() to include that base stucture.

In my v11 theme, “content.php” looks like:

>

Other Formats

Now we can create the templates for other formats just by adding the correct files. An example of my status format template looks like:

> echo ‘

‘;
the_content();
echo ‘

‘;
?>

In this case, content-meta.php just defines what meta information to display on a post.

Another example that reuses the base layout is my video format template:

>

$video = get_post_meta( $post->ID, ‘_format_video_embed’, true );
if ( !empty( $video ) ) {
$url = esc_url( $video );
if ( $embed = wp_oembed_get( $url ) )
echo $embed;
elseif ( !empty( $url ) ) {
printf( ‘‘, $url );
} else {
echo $video;
}
}

get_template_part( ‘content-base’ );

?>

Again, there is some more complex stuff here — I use a custom field called “_format_video_embed” to define what video to use. But the general idea is that it displays a video and then just grabs the base structure.

#Tips for Links

A common way of implementing link posts is to grab the first link in the post and set that as the reference for the title link. I have a function in v11 that lets you grab the first link in a post. Then you can output that as the href in content-link.php instead of using the permalink.

if ( ! function_exists( ‘v11_url_grabber’ ) ) :
/**
* Grab the first URL in a link post so we can use it as the href
/
function v11_url_grabber( $content ) {
if ( ! preg_match( ‘/<a\s[^>]
?href=\'”[\'”]/is’, $content, $matches ) )
return false;

return esc_url_raw( $matches[1] );
}
endif;

#WP Post Formats

As a final note, I like Crowd Favorite’s WP Post Formats plugin. It gives you some nice looking meta boxes for interacting with custom fields on your post formats.


  1. There’s some good documentation on adding post formats in the Codex. 

The Official James Bond site is on WordPress

This is awesome. I mean, it’s James Bond!