nabeel shahzad

Breadcrumbs for your Cake (2.1 feature)

without comments

CakePHP 2.1+ (currently in beta) comes with an awesome new feature called view blocks and view extensions (official docs here). This allows you to modify views, append to them, and change them, based on the content that might come later on in your code.

Previously, for building a breadcrumb, there was a giant if/else tree for parsing the request object, and views and helpers all structured to allow for the flexibility. In short, a nightmare. Now with view blocks functionality, it reduces it down to a very simple set of elements, which can be called in meaningful views.

View blocks are built as, you guessed it, blocks. Each block has a name (you chose it, whatever is relevant, “sidebar”, “breadcrumb”). You define a block as such:

Where do you place this? In a view file. For example, when a user goes to mysite.com/listings/add, it loads a view in Listings/add.ctp. Inside this file, at the top, you do can do:

This says that when this view (Listings/add.ctp) is loaded, then the breadcrumb should be “Home > Add Listing”. Of course this is simplified (I removed any HTML, etc, just to demonstrate).

Now that we have one view block, we need to pull this into the main layout. Since the breadcrumb shows up before any content, we will structure it using an element, which is pulled into the default layout file. I have an element, in Elements/breadcrumbs/base.ctp, which houses the basic structure of my breadcrumb. This element is called from my Layouts/default.ctp (also, super simplified):

The $this->fetch(‘breadcrumb’) is getting a block by the name of breadcrumb. I check to see if that block exists; if it doesn’t, then I output a default item of “Home”. Otherwise, I output the contents of the “breadcrumb” block.

Then when you load the page, it will show Home, and if you go to Listings/add, it will show Home > Add. Pretty simple! In my views, instead of calling the start()/end(), I have an element, under Elements/breadcrumbs/single.ctp, that builds a single level breadcrumb:

You can clean this up even more by automating it from the $this->request, and using $title_for_layout. I also have another view, which takes an array as a parameter, to build multi-level breadcrumbs:

Hope this helps! The $scripts_for_layout has also been deprecated in favor of of the view-blocks feature, which create, since now Javascript file can be included on a page-by-page basis, using $this->append

Written by Nabeel

January 31st, 2012 at 2:24 pm

Posted in CakePHP,php

Node.js and nginx

with 2 comments

This took me some time to figure out, and I didn’t see any detailed posts or bug reports on how to fix this. Nginx doesn’t support HTTP 1.1 on proxy pass, meaning, when you place Node.JS behind a proxy (for load balancing purposes, or you just have multiple endpoints on port 80), websockets will not work properly, since HTTP 1.1 is a core requirement. You’ll know, when you get errors similar to this:

I’m running nginx 0.6.8, with nginx 1.0.11. To fix this, you need to upgrade to a later version of nginx (a development version), which supports HTTP 1.1 (albeit, experimentally), and then enable the proxy_http_version 1.1 parameter in your vhost configuration.

I’m doing this on Ubuntu.

First, let’s compile nginx:

Next, we setup our vhost:

And now no more errors, and nodejs is working properly. Do note that this is a “bleeding edge” version of nginx, and could come with its own share of issues – so keep an eye out and test thoroughly!

Edit: If you’re still running into some problems, you can enable only xhr-polling/jsonp-polling in your Node.JS/socket.io configuration:

Written by Nabeel

January 21st, 2012 at 7:48 pm

Posted in General,nginx

Domain updated!

without comments

I’m now using “nabeelio.com” – since it’s been my long-time nickname, and nsslive just doesn’t mean anything anymore. All links have been updated, the old site will just redirect here. Cheers!

Written by Nabeel

January 17th, 2012 at 10:30 am

Posted in General

Sphinx and CakePHP

with 14 comments

For a project, I’ve decided to use the Sphinx search engine, and was looking for behaviors for CakePHP, to just make it much easier to implement. Since I’m using Cake 2.0, I could only find something that was for < Cake 1.3. So I decided to update it for use with Cake2.0, and it’s working beautifully with pagination.

It’s located in my github site:

https://github.com/nshahzad/Sphinx-CakePHP

The usage is exactly the same as the original (the link to it is above). The only thing is that it’s assuming you have the sphinxapi.php (which comes with the Sphinx source) extracted into┬áVendor/sphinxapi/sphinxapi.php (that’s where App::import() will look for it).

Written by Nabeel

January 3rd, 2012 at 10:01 am

Posted in CakePHP,phpVMS

Installing Redmine on Ubuntu 11.04 w/nginx and mongrel

without comments

This one took me a few hours, but I’ve got my handy-dandy notes. I’m going to assume you’re got nginx installed, whether there are vhosts or not…

I’m also installing to /var/www/redmine

Next, we are going to patch Redmine, to work with Mongrel

Next, setup the right versions of Rails, etc

Now setup MySQL:

And next, we configure and run the installer for Redmine. We are going to edit the database.yml, set it to match your above settings

Next, we start the server

Next, create the nginx vhost, I created it as /etc/nginx/sites-enabled/redmine

Written by Nabeel

October 3rd, 2011 at 6:11 pm

Posted in General,nginx

Tagged with ,

MySQL Diff Tool

with 7 comments

After searching for a while, I haven’t been able to find a tool which will show the diffs between two MySQL Databases. There are plenty to handle migrations, but migrations are tough when you’re writing an app which is install by an end-user. So I wrote a tool/class which will take the XML of a proper database (that file can be distributed in your package), and then will compare the XML schema against the schema in the current database.

Generate a MySQL Dump file:

Then call the command line script (diffgen):

There’s also a class file (which it is all from), which you can use to integrate into your own custom scripts (as-is the case with phpVMS, which is distributed with the structure.xml that is generated by my Phing build process, and it “shapes” the database on the remote server properly in an update script).

The script can be downloaded from https://github.com/nshahzad/MySQLDiff

Written by Nabeel

April 12th, 2011 at 7:52 pm

Posted in General

Amazon PHP API

with 3 comments

I couldn’t find a working PHP class for the Amazon API, which had Exception handling and some versatility. So I wrote one up. It’s a work-in-progress at the moment, but it’s available on GitHub:

http://github.com/nshahzad/AmazonAPI

The included README has detailed instructions. The class uses the __call() functionality, and you just pass the required parameters as a dictionary array. Requires some reading of the Amazon docs, but much more flexible.

Written by Nabeel

September 29th, 2010 at 9:20 am

Posted in General

PHP class for Google Geocoder API

without comments

For a project I’ve been working on, I needed to access Google’s Geocoder API. I search for names (schools in this case), and return as much info as I can that Google has about it.

I’ve posted the class up on Github, it’s straightforward, and might help some people out:

http://github.com/nshahzad/Google-Geocoder

The usage is in the readme/displayed right on the github page. It uses cURL and JSON to keep the traffic transfered low. That also means you need the json_decode() function, which is in PHP 5.2 and up.

Happy 4th!

Written by Nabeel

July 4th, 2010 at 1:44 pm

Posted in General

Building php-fpm against Ubuntu PHP Packages

without comments

This is how I’ve been building php-fpm against the Debian PHP packages. It’ll be useful for when Ubuntu 10.04 (Lucid Lynx) Comes out with PHP 5.3. I do this from my home directory. It will download the package souce from Ubuntu, then compile php-fpm standalone against that.

Then, configure the php-fpm file. That should be it :)

Written by Nabeel

April 4th, 2010 at 10:54 am

Posted in General

A better way for nginx PHP config

with one comment

Doing some reconfiguration on my webserver (nginx) to make it easier to administer. My first goal was to get rid of this nastiness:

It’s too verbose to copy/paste into each virtual host file. Instead, you can just combine the file into the /etc/nginx/conf/fastcgi_params file. I renamed it to php_params, and this is what it’s got:

Now I don’t have to change it everywhere. So, instead, now I do:

Bam! 6 lines down to one, and much easier to administer. I like, I like.

Written by Nabeel

October 6th, 2009 at 5:23 pm

Posted in General,nginx