ETags in Laravel 4 using Filters

Laravel 4 has some very nice features in which you can build your api on. After reading about some best practices for REST many people suggest using ETags for validation caching. I’ve come up with a realtivley simple solution to enable ETag validation of your api data.

I simply used a filter to catch the response after it was created, created an md5 hash of the content and compared it with ETag in the request. The code in my app/routes.php looks something like this:

Route::filter('etag', function($route, $request, $response){
  $etag = md5($response->getContent());
  $requestETag = str_replace('"', '', $request->getETags());

  if($requestETag && $requestETag[0] == $etag){
    $response->setNotModified();
  }

  $response->setEtag($etag);
	
  return;
});

Route::group(array(
    'prefix'	=> 'api/v1',
    'after'	=> 'etag'
  ),
  function()
  {
    Route::resource('invoice', 'V1\InvoicesController');
  }
);

Now when I fetch an invoice an Etag is generated, and compared with the one supplied in the request. If they match, the body is discarded and a 304 is sent back.

This obviously has a few gotchas:

  • It will not lower any server load. In fact this might slightly increase load since you’ll be generating and comapring ETags on each request. This is simply a bandwidth saving solution for you and the client.
  • I’m hashing the entire body content. For some this might be undesirable. As a minimum hash the data that you need to know if it changed since the last request.
  • This doesn’t replace other caching methods like memcached, it’s meant to work along side it.

References:
http://fideloper.com/laravel4-etag-conditional-get

Show Categories Filter on Custom Post Type List

Found this useful thread on displaying a category filter on a custom post type in wordpress. Here’s the code:

add_action( 'restrict_manage_posts', 'my_restrict_manage_posts' );
function my_restrict_manage_posts() {
	global $typenow;
	$taxonomy = 'your_custom_taxonomy_name';
	if( $typenow != "page" && $typenow != "post" ){
		$filters = array($taxonomy);
		foreach ($filters as $tax_slug) {
			$tax_obj = get_taxonomy($tax_slug);
			$tax_name = $tax_obj->labels->name;
			$terms = get_terms($tax_slug);
			echo "";
		}
	}
}

WordPress › Support » Show Categories Filter on Custom Post Type List.

Using term_description as keyword meta

I found a good use for WordPress’s taxonomy description. I’m using it to display keywords for the meta tag. When a user visits the archive page of a taxonomy the keyword meta will be displayed in the header.

Put the following code in your header file. Then make sure to put in a comma separated list of keywords in the description field of your custom taxonomy or category.

$desc = term_description( '', get_query_var( 'taxonomy' ) );

if($desc != ''){

	echo '';

}

via Function Reference/term description « WordPress Codex.

Upload issues on WordPress with Mediatemple DV server

I was having some trouble a while back uploading media to my WordPress install. I would continually get an error stating that the media couldn’t be moved to the correct folder. I had tried all the forum solutions including even changing permissions to 777. Nothing seemed to work. After stumbling across a few threads on wordpress and finding a Mediatemple community wiki article I came up with a solution. Continue reading

Nginx and Weird “400 Bad Request” Responses | Life Scaling

I noticed the other day that Google Chrome was giving me a 400 bad request when I tried to visit one of my web sites. My server is built on linode using nginx as a front end and apache as the back. No other browsers were giving me this problem. I cleared the cache and this fixed the problem, but I had a sneeking suspicion that it would happen again. Oren Solomianik of Life Scaling came up with a solution that seems to do the trick. See the link below.

trackback uri

via Nginx and Weird “400 Bad Request” Responses | Life Scaling.

Producteev PHP Library

Our organisation started using Producteev a while back for task management. It’s a pretty decent platform and has a nice api. Angelo R. from http://xangelo.ca has done some work to create a PHP Library for the api. It works well but I noticed that any time you added a space to value that you where sending to Producteev the code broke. I found the problem and have created a new library with the added fix:

Download Producteev PHP Library