Skip to content


How to fix blank screens when instantiating objects in Zend Framework

In certain cases ZF will refuse to instantiate an object and exit, mainfesting as a blank screen (or ‘silent fail’). No exceptions are thrown and nothing is logged by php. Since it fails to trigger the basic mechanisms of php error reporting this kind of error can be annoying to troubleshoot. Here’s what I’ve found:

It seems to occur when instantiating a class that extends Zend_Db_Table_Abstract or other models
It always involves syntax errors in the class
The errors are not detected
It is not related to file corruption or bad characters in the source file (Zap Gremlins/ Convert to Ascii on the source does not make any difference)
File permissions are ok

HOW TO FIX IT: Try to instantiate the class from inside a Controller/Action and see what happens. Usually php will behave normally under these conditions and correctly report the error type and the line number.

Lastly, I am not sure why this problem occurs inside models but not not controllers. Also, I haven’t struck it in ZF 1.11, only 1.10 and earlier.

Posted in zend framework. Tagged with , , .

How to add TinyMCE to a Zend_Form

I struck some annoying issues getting CKEditor to work on a recent project and switched to TinyMCE, which is faster to implement. Here’s how to make TinyMCE integrate with your Zend Forms.

1. Install

Download TinyMCE and put it somewhere the browser can find it. The important thing is that it needs to be accessible to the world, for example: /js/tinymce/, which on your server may be a path like /var/www/html/your_site/js/tinymce/.

2. .htaccess for TinyMCE

The .htaccess that sets up your index.php as the ZF bootstrap will greedily grab all requests ending in .php. It needs to exclude the php files that TinyMCE uses. So if your bootstrap code in .htaccess looks like this:

RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php

you’ll need to change it. I typically only rewrite to /index.php if the requested file really doesn’t exist. So .htaccess should be something like this:


RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1

3. Zend_Form Elements

Here’s an example of a textarea element in a Zend_Form:
Important: You must filter out all tags and attributes that you don’t want to explicitly allow. The code below will allow any html permitted by TinyMCE to be uploaded. Use this in conjunction with a quality filtering tool such as HTMLPurifier.


$node_content = new Zend_Form_Element_Textarea('node_content');
$node_content->setLabel('Node Content')
->setRequired(false)
->setAttrib('cols', 40)
->setAttrib('rows', 12)
->addFilter('StringTrim')
->addValidator('NotEmpty')
->addValidator('StringLength', false, array(0,65534));

4. View Script

Let’s assume that you use Layouts, and that you only want to use TinyMCE on a couple of pages on your site. For reasons of economy you don’t want to include the TinyMCE code on every page, so do something like this in the View Script (Note: WordPress is messing with the quote marks in this block, ensure you have single quotes after src= and before the last semicolon. The other quotes should be double quotes.):


$this->custom_head = '<script src="/js/tinymce/tiny_mce.js" type="text/javascript"></script>';
$this->custom_head .= "<script language='javascript' type='text/javascript' src='/js/customs.js'></script>";

5. Layout Script

The scripts we have added in the View Script won’t bubble up to the Layout Script unless it knows about this new variable, so the Layout Script needs to know to refer to $this->custom_head if it is set. Drop the code below inside the section of your layout script:


if ($this->custom_head) {
echo $this->custom_head;
}

6. Start TinyMCE

Create /js/custom.js and put this in it:

tinyMCE.init({
mode : "textareas",
theme : "advanced",
theme_advanced_buttons1_add_before : "separator,insertdate,inserttime,preview,zoom,separator,forecolor,backcolor"
theme_advanced_buttons2_add_before : "bullist,numlist,separator,outdent,indent,separator,undo,redo,separator,link,unlink,anchor,image,cleanup,help,code"
theme_advanced_buttons3_add_before : "hr,removeformat,visualaid,separator,sub,sup,separator,charmap"
});

…You will probably want to customise the toolbar buttons.

7. Last Steps

It is vital that you install and configure HTMLPurifier if you haven’t already. Do not attempt to write your own filter!

Posted in web development, zend framework. Tagged with , , .

/etc/hosts file on Mac OSX

I just put this together to cover a question I was asked recently. Might be useful to someone…

Where is the /etc/hosts file in Mac OS X?

The hosts file used by Mac OS X is in /etc/. Windows users sometimes have problems finding it since there’s no %SystemRoot%\system32\drivers\etc folder in the Mac OS. UNIX users might not expect /etc/ to be hidden by the Mac OS X Finder, so they often look in /Library/Application Support/

Accessing /etc/hosts

/etc/ is hidden by default. The easiest way of getting to the hosts file is to open /Applications/Utilities/Terminal.app. Then type:
sudo nano /etc/hosts

…you will need to type in your password. Your hots file will open in the text editor ‘nano.’

Adding entries to /etc/hosts

On a new line type the IP Address followed by at least one space or tab and then the hostname. Examples:
127.0.0.1 mydevsite.local
192.168.1.222 ads.shadycompany.biz

…hit ctrl + o to Save, and then ctrl + x to quit the text editor.

See here for more about using /etc/hosts for Virtual Servers: Adding an entry to /etc/hosts on OS X

Background: What is /etc/hosts used for?

The hosts file is a simple way of mapping IP Addresses to hostnames. It overrides mappings provided by the Domain Name System network. The hosts file is commonly used by network administrators to provide access to LAN resources when no name resolution is available. The other common usage is to block access to specified hosts by adding host entries resolving them to — for example — 127.0.0.1.
More Resources:
http://en.wikipedia.org/wiki/Hosts_(file)

Posted in mac osx. Tagged with , , , .

Two Nifty Terminal History Tricks

I’ve covered the ‘history’ command before, but here are two more useful Mac OS X Terminal commands…

I’ve been using the Ctrl+ R trick (Hit Ctrl + R, start typing part of the command you want. If you don’t like the first suggestion hit Ctrl + R again to show the next one). This is really handy and fast and it will return matches from the middle of the string.

And I just found another great Terminal command here: Discover the last used command beginning with anything without executing it… this one is a bit harder to use:

![needle]:p

…where [needle] is the first character(s) of the command you’re looking for. It’s not as flexible because it only shows the most recent match instead of cycling through the results, but could still come in handy.

Posted in mac osx. Tagged with , .

Adding an entry to /etc/hosts on OS X

Web Developers, Administrators of Mac Networks, and anyone who likes to noodle with OS X will probably eventually need to add hosts file entries. I use them when I’m testing a moved and/or changed version of a website at new hosting before I move the DNS.

I used to add hosts file entries manually then wonder why they didn’t seem to work. I tried rebooting, logging out, etc with varying success, until I learned this trick. On the Mac you must flush the DNS cache for the changes to take effect. Mac OS X used to have a friendly utility called NetInfo Manager, but that seems to have been removed in Leopard, so we’re using terminal commands.

Open a terminal sesion and type:
sudo nano /etc/hosts

Hit the down arrow until you get to the end of the file, and add your hosts file entry, for example:
192.168.2.33 myintranetserver.mydomain.com
… I put in an extra line break at the end. I don’t think it’s really necessary but it’s a habit from having to use Windows, which would ignore the last line in a hosts file.

then flush the cache:
sudo dscacheutil -flushcache

…the name should now resolve! Test it:
ping myintranetserver.mydomain.com

Other Info

One thing that Windows, for all its annoyances, did get right is that changes to the hosts file (in ‘c:\Windows\system32\drivers\etc’) are reflected immediately. I’d like to see Apple do something about this in OS X, perhaps just flushing the cache automatically when /etc/hosts changes.

Another handy use for /etc/hosts is to set up mapping of virtual server addresses. For example, I use MAMP on my development Mac, and create new virtual hosts entries in Apache for each site I’m working on. Then for each I just add a new entry in the hosts file, e.g.:
127.0.0.1 devclientsite7.leftcolumn.net
… then I just use devclientsite7.leftcolumn.net:8080 in Safari or Firefox to access the development site. It means my development sites don’t have to be in subdirectories, instead I just make sure the sites use a config file to figure out what their address is, and I make sure that config file is updated when the site is moved to a live system.

On OS X Tiger (10.4) and probably earlier, there was some strange behaviour with hosts file entries. You could add an entry and it would take effect, but if you had no network available, the name would not resolve and could not be used, even if it resolved to 127.0.0.1! So you have the bizarre situation of putting in addresses for virtual hosts as described in the previous paragraph, all of which resolve to 127.0.0.1, but still not being able to use them for testing and development when not connected to the Internet. If anyone can enlighten me as to how this was anything other than a bug in OS X I’d be interested…

You can also use hosts file entries to block ads (by resolving common ad server addresses to e.g.: 127.0.0.1). You can download huge lists of ad servers for this purpose. This is a good idea when you’re stuck using Internet Explorer on Windows, especially on dial-up.

Posted in mac osx. Tagged with , , , .

MacBook Pro won’t wake up from sleep

One day my new MacBook Pro 17″ wouldn’t wake up. When I opened the lid machine was running but the Desktop wouldn’t load (just a default blue Desktop background colour) and there was no response from the Mouse or Keyboard. I eventually had to hard reset the Mac.

This annoying issue can apparently be caused a few different ways, but if your Mac is new and has all System Updates applied, chances are that the issue is caused when you close the lid and then move the mac before the contents of RAM are written out. There’s a feature called Safe Sleep, enabled by default, which can preserve and restore the state of a sleeping Mac even when the battery is removed. This sounds great, but the Safe Sleep procedure can take up to 30 seconds, which is too long for me.

How to fix this annoying problem? Here’s how I switched it off:

sudo pmset -a hibernatemode 0
sudo rm /var/vm/sleepimage

…you need to supply the super user password. After making the change, reboot the Mac. The Mac should now Sleep within a couple of seconds. The second line removes the file that Sleep Safe uses to store the contents of RAM. Since you’ve switched off Safe Sleep, the space can be recovered.

Personally I don’t see the need for Safe Sleep on a Mac with a non-removable battery, but be aware that if the machine is asleep and runs out of battery power you wil lose your logged in session! I haven’t tried it, and most OS X apps will deal pretty well with this anyway, but it’s not wise to let this happen…

More details:

Apple pmset Manual
Macworld article on setting sleep modes

Posted in mac osx. Tagged with , , .