Specific file extensions in cakePHP

How to do it is scattered and buried in various places for CakePHP for various versions of the framework. Some of the older methods were complex and really wouldn’t work anymore for the latest build, which is 1.3. So I figured out that 1.3 does make it easier and I finally learned how to use http://domain.com/controller/view.xml as a request or link in a few steps. First, I need to add to routes.php in the config directory:

Router::parseExtensions('xml');

and in the app_controller, add the component, so it’ll know what layout and view to use:

var $components = array('RequestHandler');

This XML layout and view is standard with CakePHP in 1.3 but if I wanted to add a .json extension with the content-type set as “application/json” — add the json extension to the router, and then add json folder to the views/layout path. In the json folder, add default.ctp and it’s contents are:

<?php
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, max-age=0, must-revalidate");
header('Content-Type: application/json');
header("X-JSON: ".$content_for_layout);
echo $content_for_layout;
?>

And to pass parameters like http://domain/controller/view/param1/param2, I need to add the extension I want to use at the very end. So the parameter in the request, or in the link, becomes http://domain/controller/view/param1/param2.json or whatever other extension.

Also, in addition to adding the component to the app_controller, if I wanted to remove the debugging output that gets generated at the bottom of the json or xml for requests that pass ‘HTTP_X_REQUESTED_WITH’ == ‘XMLHttpRequest’ in the header, I added to my beforeRender method in the app_controller.

public function beforeRender() {
if ($this->RequestHandler->isAjax())
Configure::write('debug',0);
}

OSX, XAMPP and the memcache php modules

As a side note, php has two “memcache” modules, and this question via serverfault answers what the differences are between the two.

On my development machines, I use XAMPP and I recently needed to use the memcache and memcached modules. Unfortunately, XAMPP doesn’t come precompiled with them, I guess they aren’t popular modules. So after a couple of google searches and reading through ApacheFriend’s forums, I decided I had to build and install them from scratch.

First, I had to make sure I had xcode and the developer tools from Apple installed. If it’s not on the machine, download it from Apple’s developer site — http://developer.apple.com.

Along with the installation of XAMPP, I also installed the XAMPP developer package to get access to the configurations and other tools such as phpize.

Next, need to make sure the “PHP” binaries in /usr/bin points to XAMPP binaries rather then the one Apple installs on the computer. I made them all symbolic links so when I update XAMPP, I don’t have to do anything other then make sure the files exist.

Next, set up your compiling environment. For XAMPP 1.7.3, everything is precompiled using the i386 but on my machine any code compiled will use the default x86_64 architecture. To modify the environment settings, I ran the following:

$ MACOSX_DEPLOYMENT_TARGET=10.6
$ CFLAGS="-arch i386 -g -Os -pipe -no-cpp-precomp"
$ CCFLAGS="-arch i386 -g -Os -pipe"
$ CXXFLAGS="-arch i386 -g -Os -pipe"
$ LDFLAGS="-arch i386 -bind_at_load"
$ export CFLAGS CXXFLAGS LDFLAGS CCFLAGS MACOSX_DEPLOYMENT_TARGET

Now, I could finally install the modules.

PHP module: memcache

Download the memcache source module using PECL:

$ pecl download memcache
$ tar xzf memcache-2.2.5.tgz
$ cd memcache-2.2.5
$ ./configure --with-php-config=/Applications/XAMPP/xamppfiles/bin/php-config
$ make
$ sudo make install

Modify the php.ini file (/Applications/XAMPP/xamppfiles/etc/php.ini) and add:

extensions=memcache.so

Restart apache. Voila. If you do a phpinfo, you’ll see the module settings under the memcache header.

PHP module: memcached

This module’s compilation process is a bit trickier because it requires the libmemcached library installed. You’ll need to download the source from http://libmemcached.org — as of this writing 0.41 is the latest version used.


$ tar xzf libmemcached-0.41.tar.gz
$ cd libmemcached-0.41
$ ./configure --prefix=/usr/local
$ make
$ sudo make install

Next, use pecl to download the memcached source module and compile it.

$ pecl download memcached
$ tar xzf memcached-1.0.2
$ cd memcached-1.0.2
$ ./configure --with-php-config=/Applications/XAMPP/xamppfiles/bin/php-config --with-libmemcached-dir=/usr/local
$ make
$ sudo make install

Again, just like the memcache module, need to add to the php.ini file:

extensions=memcached.so

Then, again restarting apache and verifying with phpinfo that the module successfully was created.

Flash CS5 and FlashBuilder

After first hearing about the format in 2008, Flash CS5 now allows you to save everything in XML rather then the proprietary FLA format. It’s called the Flash Interchange Format and uses the XFL extension. So what does the format let you do? In theory, you can use this format with any of Adobe’s products — you can edit a bitmap images directly in Photoshop or edit videos directly After Effects, and go back to Flash without taking extra time to do something special with it. Besides those things, you could be like me and want to use it in Subversion or Git, which is a nice way of version control.

After putting in some graphics and custom animated objects in Flash, I usually sew everything up with actionscript and my editor has been FlexBuilder or FlashDevelop. FlashBuilder is more integrated then FlexBuilder — so I tried it out. I saved a flash file as XFL and added a Main as my Document Class. To test the setup, I added a trace statement to the constructor of the Main class, then ran Debug. No trace occurred.

What happened? Well, it works if I save in FLA format, but for XFL I had to do some changes. For the Document Class, I had to change it to use __FILENAME__.Main and re-ran the debug. It worked!

My next problem? I don’t want my actionscript files in my main folder. I want it in a format like this –

All my source files in the src directory. In order to do this, I have to edit the ActionScript Settings found in the Properties panel or use goto the Script option in the Flash tab using Alt (Option)+Shift+F12. Then add the folder location to where you’re going to keep your files. While I was in here, I also edited my Library location to where all my SWC files are located.

Git and Subversion Workflow – Part 2

I’ve been working with Git and Subversion together for some while, and I’ve been improving my work flow as I use it. Here’s the latest things I’ve worked out.

Stash popping with a dirty files
I use the stash command pretty frequently — often when I’m just working with a design but want to try something different and don’t want to play around with rebasing a commits — so I like my changes but want to put merge the code I had stashed. If you try:

$ git stash pop

You’ll get the error about working with dirty files. In order to remedy this, I fake it out, by pretending to want to add to the commit:

$ git add [files_i_changed]

Then, I do the stash pop and then reset so I don’t accidentally commit the code until I’m ready.

$ git reset HEAD

Appending commits for one commit to Subversion

A particular strength of Git is the ability to do multiple commits without sending everything to the server. (Okay, that’s the strength of any DVCS.) I usually like to send it all in one Subversion commit but I still want to keep a log of all my git commits, here is what I do. First, I’m working from a local branch that has all my git commits called ‘local_commits_for_issue_12345′.

Now, I will need to checkout the remote branch or the trunk that I want to commit to:

$ git checkout remotes/[branch or trunk]

And make a branch:

$ git checkout -b [some_new_remote_branch_name]

Then I merge:

$ git merge local_commits_for_issue_12345 --no-commit

Note, the no-commit part of the merge. Basically, Git will merge the code, and if it merges “successfully”, it will gracefully “fail” leaving the files that it merge in the filesystem until you do a git add and git commit and then git svn dcommit to send to Subversion. If it fails to merge successfully, well, you have to fix it like any other failed merge. As a side note, if you don’t include the no-commit, it’ll automatically commit the changes from your local branch.

Posted in Git

Upgrading to the latest linux distro version

If you upgrade your distro over the network like I do, reboot and then see the console say something like this:

mountall:/proc: unable to mount: Device or resource busy
mountall:/proc/self/mountinfo: No such file or directory
mountall: root filesystem isn’t mounted
init: mountall main process (736) terminated with status 1
General error mounting filesystems.
A maintenance shell will now be started.
CONTROL-D will terminate this shell and re-try.
Give root password for maintenance
(or type Control-D to continue):

Upgrade your linux kernel is greater than or equal to 2.6.26.8 — or else you’ll have a panic attack like I did. I’m so lucky that my service provider had it and could do it automatically.

Git and Subversion Workflow – Part 1

Setting up git to use with subversion

Creating the initial subversion repository as a git repository, run this command:
$ git svn init -s [repository_url]

Then…
$ git svn fetch

This command will probably run for a while (especially large repositories) as it needs to find branches, tags, etc.

Switching to branches and creating local branches

This command will show you all your local and remote branches. Any remote branches will always have “remote/” before it.
$ git branch -a

This command will checkout your subversion code.
$ git checkout remotes/[subversion directory]

Then, to create a branch from that remote version.
$ git checkout -b [name_of_new_branch]

Committing to subversion

After some code changes, you can commit to whatever branch or trunk folder you’re current working off of by running the following command:

$ git svn dcommit

Cleaning up git

Run this command after you’ve had the repository running for awhile or if you run into some problems. This will do some compression and clean up of the git objects.
$ git gc

Adding a remote repository


$ git remote add [repository_name] [repository]

Then to add to it:


$ git push [repository_name] [branch]

Changing eclipse defaults

Eclipse Icons
Changing the eclipse icons on a Mac. Right-mouse click on the eclipse application and choose “Show Package Contents”. Click on Contents and then the Resources folders. Here you just replace the Eclipse.icns.

Eclipse Splash Image
First, get the Random Splash Screen plugin and install into your Eclipse plugins folder. Restart Eclipse and then go to the Preferences window, and in the General section you should see the Random Splash Screen option, where you can select whatever picture you’d want to use.

References: The Flash Blog

Virtual Hosts and XAMPP on OSX

I recently encountered a problem when I was attempting to add a new virtual host in my XAMPP setup, where I tried to restart the server and apache failing because I would get the following error.

/Applications/XAMPP/xamppfiles/bin/apachectl: line 70: ulimit: open files: cannot modify limit: Invalid argument

I found the problem is that one of the paths I had set, either the DocumentRoot, ErrorLog, and CustomLog didn’t actually exist or was not writable. Once I found the missing path and fixed it, Apache restarted without a problem.

Posted in OSX

Git and Subversion together

Subversion has been around for awhile as a version control system and I’ve been using it since it became a 1.0 application. Recently, I’ve begun using Git and found that I like it a lot more because it allows more finer control over my local code with things like local branching, fast branch switching, local commits, rewriting local commits before submit to main repository, and the stash.

So with the Git SVN bridge, you can pull from your subversion repository. Be forewarned though, if you have a large subversion repository with lots of revisions, it will take awhile to do the following because Git needs to process the revisions of the entire repository.
$ git svn clone [SVN_REPO_URL] --tags=tags --branches=branches --trunk=trunk

Now you have a local repository with changes with the above Git command. What I want though is a repository where I can make changes on more than one machine because I code on a work computer, a home desktop computer and a home laptop, I can create a personal repository that would sync all my computers but not commit changes to subversion yet. To do that I would create a personal Git repository, then add a remote site to the subversion clone from last command.
$ git add remote [NAME] [PERSONAL_REPO_SITE]

Push the subversion clone to my personal repository:
$ git push origin master

Once that’s done, I can pull from my local repository on any of my computers. Any changes that I want to commit to subversion, I go back to the computer with the subversion clone, update from my personal repository and commit those changes for everyone to see.
$ git svn dcommit

That’s it — that would be hard to do with Subversion alone and Git actually works the way I work. Sweet!

Reference:
Git SVN Crash Course
Git-SCM Book

Hardcopy Reference:
Pragmatic Version Control Using Git