Fixing symlinks in Mac OS 10 + built in Apache

The other night I was fiddling about with my macbook, trying to setup a “MySQL-Apache-PHP” server on my system. Digging a bit about the system and googling a bit, it seems that the system comes with a built in apache server and php installation.

I thought to myself, why not use the built in server instead of installing a MAMP package?

Anyways, seconds went by and I found the option in the system preferences, turned on “web sharing” and excitedly headed on to localhost. It worked perfectly, now I just needed to set it up as it was on my linux desktop.

The apache’s httpd.conf file is located in the folder: /private/etc/apache2/httpd.conf
(I used textmate to edit the file.)

What I wanted to do was, since I’m the only user on this laptop, I wanted my Sites folder to have a www “folder”, which was a symlink to a folder in my Documents folder. The symlink would then point to the webapp I was currently coding.

This worked on my linux desktop, so it should also work here.

The first thing I did was to fix the DocumentRoot so it now points directly to my /Users/username/Sites/www folder (or actually the symlink, which we’ll create now).
The I added PHP support by removing the comments from line 116, containing:

LoadModule php5_module         libexec/apache2/libphp5.so

Then I went into my Sites folder and created a symlink to a folder in the Documents folder called “current_project” (yes yes, secret stuff here). This is done using the following command:

ln -s ~/Documents/current_project ~/Sites/www

After that, just reboot you apache server (you can use the Web Sharing option in the System Preferences. Just uncheck the web sharing checkbox and recheck it again).

An here my problems began. I got a lot of warnings in my apache error log (/private/var/log/apache2/error_log) saying:

Symbolic link not allowed or link target not accessible: /Users/username/Sites/www

After googling for a while with no answers, I thought : “why not disable the userdir module? I’m not going to use the localhost/~username anyways”

This turned out to do the trick.

The solution was simply to disable the userdir module in apache.
Comment out line 112 (LoadModule userdir_module libexec/apache2/mod_userdir.so) and line 465 (Include /private/etc/apache2/extra/httpd-userdir.conf)
Restart the apache server (web sharing thingy in the System preferences interface) and you server can now access the symlink.

I hope this will help you on your way, when coding using a Mac.

Small update: (2010-06-02 at 13:15)
Remember to right click the Documents folder and choose “Get Info”. Unfold “Sharing & Permissions” and set “Everyone” to “Read only”, else you will get a permission denied

(php) php-mvc-base project released

Whenever I create a new PHP project I use a certain base structure.
This structure allows me to have pretty URLs and have a nice way of configuring the database connections. Furthermore this structure also allows me to use a form of plugins in my system.

I have even included an example when you start your server, once the base structure is installed. Go check it out on http://github.com/jimmiw/php-mvc-base

Hooking up Ctrl+S using JS+Prototypejs

Did you ever find yourself in a position where having Ctrl+S hooked up as an event would be great? Well I have and doing a fast search on google gave me this page OpenJs.com – Keyboard shortcuts.
While this page is great, I needed to have a script that was written in prototypejs, since this is what we use at work (It’s a left over from the early Rails days, where prototypejs was tha bomb).

Since this really isn’t a big thing to do in prototypejs and if you use firebug you could inspect the event object in the dom yourself, I’ll just give you the script:

$(document).observe('keypress', function(e) {
  if(e.charCode==115 && e.ctrlKey) {
    e.stop();
    console.log('CTRL+s was called!');
  }
});

The only important thing to remember, is to call e.stop(), as this prevents the browsers “Save Page” functionality from opening.

(php) flash-message plugin

For a long time I’ve wanted a way to send messages from my controller to my view, when posting data. The message could be “Your update went well” or some error.

In the old days, I have done it like this:

controller, users/update.php:

<?php
$result = mysql_query("update users set email = '".$_POST['email']."' where id = ".$_POST['id']);

if($result) {
  header("Location: /users/index.php?message='Some long message, that might bug out'");
}
else {
  header("Location: /users/edit.php?message='ERROR because....'");
}

?>

view, users/index.php and users/edit.php:

<?php
echo $_GET['message'];
?>

However, these days are over now. With my little plugin, you can easily pass messages from the controller to the view.
All you need to do, is to include the script on your pages, then call: flash(label, message) from your controller. From your view, you can call echo getFlash(label) and that is it!

Using the same example from before, we get the following:

controller, users/update.php:

<?php
$result = mysql_query("update users set email = '".$_POST['email']."'  where id = ".$_POST['id']);

if($result) {
  // set the message
  flash('success', 'Some long message, that will NOT bug out');
  // do the redirect
  header("Location: /users/index.php");
}
else {
  // set the message
  flash('error', 'You had an error in your data.');
  // do the redirect
  header("Location: /users/edit.php");
}
?>

view, users/index.php:

<?php

echo getFlash('success');

?>

view, users/edit.php:

<?php

echo getFlash('error');

?>

Head on over to the github reposition at get it: http://github.com/jimmiw/php-flash-message

I hope you enjoy it

Use Rails’ form_tag with care

Today at work I was fiddling with some forms in an admin interface. When I had to make an update form, the data didn’t hit the correct controller action.

If you ever get the message ActionController::MethodNotAllowed, then you’ve created your form wrong!

The error came when I tried doing:

<% form_tag :action => :update, :method => :put do %>
  lots of fields here
<% end %>

The data was not sent correctly (it was sent as POST, not as PUT as I wanted) and the action was not found. After googling for a bit, I found out that others had a similar problem.

The answer was rather simple though. In the rails docs you use the form_tag like this:

form_tag(url_for_options = {}, options = {}, *parameters_for_url, &block)

What I didn’t realize straight away was, that my :method => put was being sent in the first hash and NOT the second as it was suppose to.
So if you are creating your own forms, without activerecord, do it like this:

<% form_tag({:action => :update}, {:method => :put}) do %>
  lots of fields here
<% end %>

You gotta design!

Updated 2010-01-19 23:59: See new images at the bottom of the post!

After seeing a tweet from @smashingmag about designing a small piece of graphics everyday, I and @janusclemmensen decided to try it out.

Here is my contribution for day11:
day11
Still learning gimp. Perhaps this is a good place to start.

Contribution for day12:
day12

Gonna try to make the design in gimp for tomorrows contribution. Here’s the sketch though!
Contribution for day13:
day13

Got a tad stressed for time today, so i’ve just done some messing about in the gimp.
Contribution for day14:
day14

Weekend and just a tad stressed at work, but now i’m back!
Submitting a picture of a cake i’ve baked. 2 layers of banana cake (yes, both the yellow and blueish is banana cake)
Contribution for day19:
day19

Snap Open forked!

I have been using Snap open in gEdit for quite some time and I love it. It’s fast and easy to use, plus it saves me from using my mouse. However, the “fast” part seems to loose it’s grounds, when you start using it in larger projects. I even started to have problems on java projects.

After some investigation, I found out that it also looked in my target folder. The .snap_open_ignore file helped a lot here though. But still the search was slow and some times the search window stalled for longer periods.

I decided to fork the current project on github and fix the problems myself. As you can see from the project data, the last commit date was around November 8th, 2008. So I assume the author sees it as complete. (meaning I will not try to submit patches to him).

Anyways, the problem with his version was, that everytime you typed, the search feature would do a file search on the disk. So, every time I typed a new letter, the plugin did a search… everytime. So.. I fixed this. Instead of doing the search every time I typed, it’s now doing it only once – The first time you open the window. This increased performance a LOT. It’s now a lot smoother to use the snap open plugin.

So head over to github.com/jimmiw, and check my version

Overriding Firebug

The other day at work, I deployed a test release of a product to the test servers, which ofcourse gave some problems… It seems that our CEO’s and other devs didn’t have Firebug installed which caused the app to stop loading. The reason for this is, that I like the console.log() feature, which I use intensively while developing apps.

I then started to uncomment all my console.log() statements before deploying to test servers, but it seemed rather stupid, and the only other alternative was to get the CEO’s and devs to install firebug (and to make them stop using IE for matter). *cough* I decided to find a way to override the console.log() command instead, and I just wanted to share it with you.

try {
	console.log("console.log test"); 
}
catch(e) {
	console = {
		log: function(a) {
			// blank..
		}
	};
}

Just put this tiny piece of code at the very top of you file, then it overrides the console.log() command, and all is well! Tested in IE6+7, Opera 9.25 and Firefox (without firebug installed).