Install CakePHP in a subdirectory of WordPress

Let’s say you have a WordPress installation which is accessible in the browser at example.com and you want to install CakePHP in a sub-directory (or a directory inside a sub-directory) within WordPress so that it is accessible at something like example.com/cake/ or example.com/project/cake/ in the browser.

To achieve this, you need to modify the .htaccess files used by WordPress as well as CakePHP. I’ll explain how to modify .htaccess files in the following steps.

Step 1: Modifying .htaccess file in WordPress

In the directory where WordPress is installed ( in the same folder as wp-config.php file ), edit the .htaccess file and add the following line, assuming that your CakePHP subdirectory will be called ‘cake’

RewriteRule ^cake/(.*)$ /cake/$1 [L,QSA]

so that your .htaccess file looks like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /
 RewriteRule ^cake/(.*)$ /cake/$1 [L,QSA]
 RewriteRule ^index\.php$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . /index.php [L]
</IfModule>
# END WordPress

If your ‘cake’ is going to be inside another sub-directory of WordPress installation called ‘project’, then this line should look like:

RewriteRule ^cake/(.*)$ /project/cake/$1 [L,QSA]

If you don’t do this step, visiting example.com/cake/ or example.com/project/cake/ will try to open as a WordPress page which, obviously, isn’t right and will fail.

Step 2: Modifying .htaccess files in CakePHP

After completing Step 1, you will need to add the following RewriteBase statement in the .htaccess files used by CakePHP. If you don’t complete this step, your CakePHP app will show up but fail to load CSS / JS or any other assets in /app/webroot.

RewriteBase /cake

So, first of all, navigate to /cake/ or /project/cake/ , whichever is your CakePHP folder and edit the .htaccess so that it looks like this:

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteBase /cake
 RewriteRule ^$ app/webroot/ [L]
 RewriteRule (.*) app/webroot/$1 [L]
</IfModule>

Then navigate to the ‘app’ folder inside ‘cake’ and edit the .htaccess file so that it looks like this:

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteBase /cake
 RewriteRule ^$ webroot/ [L]
 RewriteRule (.*) webroot/$1 [L]
</IfModule>

Finally, navigate to webroot folder inside this ‘app’ folder so and edit the .htaccess file so that it looks like this:

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /cake
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

That’s it. You’re good to go.

Visiting example.com in the browser will open your WordPress website, while example.com/cake/ or example.com/project/cake/ , depending on how you configured it, will open your CakePHP app.

Fix HTTP 500 Internal Server Error on renaming CakePHP root directory

If you have to rename your CakePHP root folder for some reason, you might come across HTTP 500 Internal Server Error. This generally happens because of PHP opcode caching done by PHP accelerators for performance boosts.

The problem can usually be fixed by restarting your server. If not, you might have to clear the PHP opcode cache manually.

Clearing PHP opcode cache in XCache
Clearing PHP opcode cache in XCache

If you’re using MAMP (or MAMP Pro) on a Mac, which comes bundled with XCache, you can easily fix it by visiting XCache tab from server start page and using the clear button against the PHP cache. If you have a different PHP dev setup, check for other PHP accelerators like APC or eAccelerator.

This will fix your problem and your CakePHP app will continue to function normally after renaming the CakePHP root directory.

Bug fix for undefined variable notice in CPT-onomies 1.1.1

CPT-onomies WordPress Plugin by Rachel Carden enables you to use your custom post types as taxonomies and create relationships between your posts by using custom post type post titles as taxonomy terms, in a similar fashion as you would create taxonomy relationships.

Since it is not yet possible to create relationships between custom post types like that with WordPress, out of the box, I am using CPT-onomies v1.1.1 for a project that I’m presently working on.

While playing around, I stumbled across a bug which introduced a debug notice in the WordPress dashboard and didn’t let me publish or update posts for those custom post types which used a cpt-onomy (custom post type as a taxonomy). I reported it to Rachel and she responded quickly with a fix.

CPT-onomies v1.1.1 bug

Until a new version of the plugin comes out with the fix, to get rid of the debug ‘Notice’, you can change line 931 of manager.php in the plugin root directory from:

'cpt_onomy_archive_slug' => ( isset( $cpt_settings[ 'cpt_onomy_archive_slug' ] ) && !empty( $cpt_settings[ 'cpt_onomy_archive_slug' ] ) ) ? $cpt[ 'cpt_onomy_archive_slug' ] : NULL );

to

'cpt_onomy_archive_slug' => ( isset( $cpt_settings[ 'cpt_onomy_archive_slug' ] ) && !empty( $cpt_settings[ 'cpt_onomy_archive_slug' ] ) ) ? $cpt_settings[ 'cpt_onomy_archive_slug' ] : NULL );

This will remove the notice and you’ll be able to publish or update posts of custom post types which use a cpt-onomy without a hassle.

Getting Port 80 for Apache to work in MAMP or MAMP Pro

Setting Apache to use Port 80 in MAMP
Setting Apache to use Port 80 in MAMP

If you’re stuck with MAMP (or MAMP Pro) unable to use the default Apache and MySQL ports (Port 80 and Port 3306 respectively) when you change them in preferences, use the following steps to ensure that the changes you make are saved and work:

  1. Launch MAMP. Open Terminal by typing terminal into Spotlight (Command + Space).
  2. Open MAMP Preferences (Command + , ) and click on Reset MAMP Ports (Port 8888 and Port 8889 for Apache and MySQL respectively). Click on OK.
  3. Switch to the terminal. Type sudo apachectl stop to shutdown the system Apache.
  4. Restart MAMP.
  5. Open MAMP Preferences once again and click on Set to Default Apache and MySQL ports. This will set the Apache and MySQL ports to 80 and 3306 respectively.
  6. Switch to the terminal. Type sudo apachectl restart to restart Apache.
  7. Switch back to MAMP and click on Open Start Page (or go to http://localhost/MAMP/?language=English in your browser)

And you’re done.

By using Port 80 for Apache HTTP server, instead of having a URL like http://localhost:8888 you’ll have a clean URL like http://localhost

This is useful in certain cases, for example WordPress multi-site installation where you cannot create a network if “WordPress address (URL)” uses a port number other than ‘:80’, ‘:443’.

Setting preferences in MAMP usually works without a hassle but I encountered this problem, today morning. Only the default MAMP ports seemed to work, no matter what I set in the preferences. This led to a lot of wasted time and productivity. I hope this how-to guide saves you some hair and valuable time.

Humanoid Robot NAO performing Sun Salutation

As a Research Intern at Gade Autonomous Systems, I had the opportunity to implement the Human-Robot Interaction module on a NAO humanoid robot to promote linguistic and social learning in children with autism spectrum conditions.

Among other things, I programmed NAO to perform the Surya Namaskar, the traditional Indian practice of Sun salutation. The video starts a bit shaky but improves in a few seconds:

For development, I used Python for programming and Choreographe by Aldebaran Robotics running on a Ubuntu 11.10 machine. The behavior was simulated on NAOsim powered by Cogmation Robotics running remotely on a Windows 7 machine.

I utilized all the three methods for integrating motion to develop this particular routine, namely:

  1. The Timeline Editor
  2. The Recording Mode
  3. The Motion API (ALMotion)

Each of these methods have their own pros and cons. For example,

  • Programming the exact actuator position values using the Motion API is time consuming but allows for unparalleled precision
  • The timeline panel or the motion editor allows for defining frames quickly and specifying the animations in those frames
  • The recording mode allows you to move NAO’s body to a certain position manually and record the actuator position values

By building this behavior, I learned about the well designed interface between the simulator (NAOsim) and the development suite (Choreographe and Telepathe). The number of times NAO fell on his face in the simulator highlighted the importance of extensive testing and simulation with in a  physics-enabled environment before deployment in the wild. My understanding about the synchronous and asynchronous flows improved greatly using the box-based GUI and, eventually, I programmed the flows using the API as well.

An idea from my dad, and observing the locals practice their tradition inspired me to build this particular behavior into the initial Human-Robot Interaction module. Hope you enjoyed watching the video.

Me, teaching Surya Namaskar to NAO Humanoid Robot

pyblobs with OpenCV 2.1 on Ubuntu 11.10

pyblobs is a Python interface for cvBlobsLib. To use it with OpenCV 2.1 on Ubuntu 11.10 follow these steps.

Prerequisites

The following dependencies are required to use pyblobs library:

sudo apt-get install swig
sudo apt-get install libcv-dev
sudo apt-get install python2.7-dev
sudo apt-get install python-opencv

If you are using Python 2.6, then use sudo apt-get install python2.6-dev instead.

Getting the pyblobs library

Use svn to grab the source code

cd /path/to/Downloads/
svn checkout http://pyblobs.googlecode.com/svn/trunk/ pyblobs-read-only

Patching the files

The code from the trunk as of now uses Python 2.5 (python2.5-dev) and not the best way to link shared objects in the make_swig_shadow.sh file. There is another issue with building the pyblobs library to be used with OpenCV 2.1 in the file BlobExtraction.cpp

Hence, I have created two patch files for the aforementioned files:

Download these to the same directory as the downloaded library (pyblobs-read-only) and then run the following commands in the terminal:

cd /path/to/Downloads/pyblobs-read-only/
patch make_swig_shadow.sh make_swig_shadow.patch
patch BlobExtraction.cpp BlobExtraction.patch

If you are using Python 2.6, then after patching edit make_swig_shadow.sh and replace any occurennces of python2.7 with python2.6 .

Installation

To install

make
./make_swig_shadow.sh

To check your install, go to your Python shell and try the following imports:

>>> from blobs.BlobResult import CBlobResult
>>> from blobs.Blob import CBlob

If all goes well, you shouldn’t see any errors. Now you can work with pyblobs and OpenCV2.1 on Ubuntu 11.10!

That said, to get the example file demo.py working, you might need to modify it a little to use objects and methods from the newer OpenCV Python API. For example, in my programs I replace

from opencv import cv
from opencv import highgui

with import cv2.cv as cv to use the new API as cv.MethodName() instead of cv.cvMethodName() or highgui.cvMethodName(). Makes life a lot easier!