Brian Wigginton Lab and Nerdery

Filename Cache Busting with CakePHP

Posted on July 18, 2012

This assumes you're using CakePHP 2.x

CakePHP let's you use timestamp based cache busting on your assets by enabling Asset.timestamp in your core.php file. By enabling this and using the builtin HtmlHelper::css and HtmlHelper::script methods, your assets will have a timestamp appended to them as a query string. For example:

/app.js?123456789

However, older squid proxies will not cache anything that has a query string, thus preventing your files from being cached. An alternative solution is to move the timestamp into the filename, and then use htaccess or similiar rewrite rule system to map those files back to the actual files on disk. The end output would be something like the following:

/app.123456789.js

To implement this in CakePHP, you need to override the assetTimestamp in AppHelper.php

<?php

class AppHelper extends Helper {

/**
* Override so that the timestamp is added to the file instead of as a query parameter,
*/
public function assetTimestamp($path) {
$parts = explode('?', parent::assetTimestamp($path));
return substr_replace($parts[0], '.'.$parts[1], strrpos($parts[0],'.'), 0);
}

}

view raw AppHelper.php This Gist brought to you by GitHub.

Use the following in your .htaccess file. If you're not using Apache, see the html5 boilerplate for nginx or web.config files.

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.+)\.(\d+)\.(js|css|png|jpg|gif)$ $1.$3 [L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
view raw .htaccess This Gist brought to you by GitHub.

Read more about filename cache busting at the Html5 Boilerplate Wiki entry on cache busting.

Filed under: CakePHP, PHP No Comments

Magento Sidebar Categories

Posted on January 17, 2010

The following will display a list of categories in your store in your right sidebar.

Layout

Alter page.xml so that the default handle's right block contains the following:

<block type="catalog/navigation" name="category.listing" template="catalog/navigation/categories.phtml" />

My final right block looks as follows

<block type="core/text_list" name="right" as="right">
	<block type="catalog/navigation" name="featured" template="catalog/featured_random.phtml" />
	<block type="catalog/navigation" name="category.listing" template="catalog/navigation/categories.phtml" />
</block>

Template

Create the file template/catalog/navigation/categories.phtml and paste in the following

<div class="block block-layered-nav">
  <div class="block-title">
    <h2>Categories</h2>
  </div>
  <div class="block-content">
    <ul id="nav_category" class="nav_category">
    	<?php foreach ($this->getStoreCategories(true) as $_category): ?>
    		<?php echo $this->drawItem($_category) ?>
    	<?php endforeach ?>
    </ul>
  </div>
</div>
Filed under: Magento, PHP 4 Comments

Magento Sidebar Featured Products

Posted on January 17, 2010

This tutorial will explain how to add a featured product to the sidebar. In this example we will add the featured product to the right sidebar. The steps we will follow are as follows.

  1. Create a new category in the magento admin area to contain our featured products.
  2. Add the block calls to the XML Layout
  3. Create the phtml template files to be referenced by the block

Category

Add a new category to your store to contain your featured products. Make it inactive and jot down the id that it is given once saved. Add any products you want to consider featured to this category. Also make sure this category is not a root category.

Layout

Open page.xml and inside your default handle's right block add the following line.

<block type="catalog/navigation" name="featured" template="catalog/featured_random.phtml" />

My finished right block looks as follows:

<block type="core/text_list" name="right" as="right">
	<block type="catalog/navigation" name="featured" template="catalog/featured_random.phtml" />
	<block type="catalog/navigation" name="category.listing" template="catalog/navigation/categories.phtml" />
</block>

Template

Use the following code for featured_random.phtml

<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE_AFL.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category   design_default
* @package    Mage
* @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
* @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*/
?>
 
<?php 
$category_id = "25"; // category_id for "Featured Products"
$_productCollection = Mage::getResourceModel('catalog/product_collection')
  ->addAttributeToSelect(array('name', 'price', 'small_image'), 'inner')
  ->addCategoryFilter(Mage::getModel('catalog/category')->load($category_id));
?>
 
<?php if($_productCollection->count()): ?>
 
  <?php 
  $products = array();
  foreach ($_productCollection as $_product) { array_push($products, $_product); }
  $_product = $products[rand(0,count($products)-1)];
  ?>
<div class="block block-featured-product">
  <div class="block-title">
    <h2>Featured Product</h2>
  </div>
  <div class="block-content">
    <ul id="featured-product-list">
      <li class="featured-product">
          <h4><?php echo $this->htmlEscape($_product->getName())?></h4>
          <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
            <img class="product-image" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(117, 117); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
          </a>
          <a class="view-item-button" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">View Item</a>
      </li>
    </ul>
  </div>
</div>
<?php endif; ?>

Resources:

Filed under: Magento, PHP 13 Comments

Subversion: Continuous Integration with a PHP Application

Posted on October 27, 2009

Abstract

This tutorial explains how to update a staging site whenever there is a commit to the SVN repository. This assumes basic knowledge of Subversion, Apache and C and that the staging site is running a working copy checked out from the repository.

Problem & Solution

When working close with designers or other non-coders it's often most productive to be able to make quick changes live to the box serving up the dev site, then to commit a change then update the server over and over. The bigger issue is that there is now modified code on the dev site that's not revisioned with subversion. To solve this problem we are going to update the codebase for the development site whenever anyone commits changes to the subversion repository.

update_svn

update_svn is a C program that will make a call to the svn command with our designated username and password and update the working copy. This program should live inside your applications directory.

update.c

#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
     execl("/usr/bin/svn", "svn", "update", "--username",
     "USERNAME", "--password", "PASSWORD",
     "/var/www/website/"(const char *) NULL);
     return(EXIT_FAILURE);
}

Compile and add permissions

gcc svn_update.c -o /var/www/website/update_svn
chmod +s update_svn

Subversion post-commit hook

In our subversion repository's post-commit script we want to add a call to update_svn. Add the following line to your post-commit script:

/var/www/website/update_svn

Resources:

Filed under: PHP, Subversion No Comments
   
Google+