Quick Tip: are you using pr()?

Tuesday 1 September 2009 12:43 , In , ,

pr() is a version of print_r() with <pre> tags wrapped around it. It will echo out your array in a human-readable format - without having to view source.

Why is this useful? Well, in the world of CakePHP, everything is an array and eventually you're going to have to do some debugging. Not only is pr() easier to type, it's

Why doesn't pr() work?



Have you set debug mode to zero?

Check your /app/config/core.php for this line:

Configure::write('debug', 0);


Change it to:

Configure::write('debug', 2);


And now you should get some nice debug output at the bottom of every page too.

10 great programming fonts

Thursday 27 August 2009 02:21 , In , ,

Programmers spend hours at their desks either working hard or just trying to look like they are. As a programmer your eyes will take a pounding and the font you use is important.

You don't have to stick with the default terminal font, or whatever your IDE uses. Try some of these fonts and give your eyes a rest.

Remember: bump up the font size and your eyes will thank you for it.

  1. Inconsolata - download


  2. Consolas - download


  3. Deja Vu Sans Mono - download


  4. Droid Sans Mono - download


  5. Proggy - download


  6. Monofur - download


  7. Profont - download


  8. Monaco - download


  9. Andale Mono - download


  10. Courier - download


Using CakePHP with your IDE

Friday 5 June 2009 03:26 , In , ,

CakePHP 1.2.x uses .ctp files by default which means you will probably lose syntax highlighting in your chosen editor. CakePHP 1.1.x uses .thtml files.

In this tutorial we'll show you how to set up syntax highlighting for .thtml and .ctp files.

CakePHP and Eclipse


To set up your version of Eclipse to read either file type, follow these steps:

  • Go to Window > Preferences > General > Content types
  • In the content types window navigate the tree to text > PHP content type
  • Click add
  • Enter *.ctp or *.thtml depending on what version of CakePHP you use. It might be best to add both - just in case.
  • Now navigate to Editors > File Associations
  • Enter *.ctp or *.thtml depending on what version of CakePHP you use.
  • Under "Associated Editors:", select the PHP Editor and click Default.


CakePHP and Dreamweaver


  • Go to your Dreamweaver installation folder (let's assume this is C:\Program Files\Adobe\Adobe Dreamweaver CS3)
  • Open the configuration folder
  • Edit extensions.txt and modify the first line so THTML and CTP are present (comma seperated. It should look something like this: ,MASTER,THTML,CTP:All Documents
  • Edit the PHP files line in the same way. It should look like this: PHP,PHP3,PHP4,PHP5,TPL,THTML,CTP:PHP Files
  • Now browse to the DocumentTypes folder and open MMDocumentTypes.xml.
  • Do a ctrl-f search for "winfileextension" and modify the line to look like this: winfileextension="php,php3,php4,php5,thtml,ctp"
  • Do the same for "macfileextension": macfileextension="php,php3,php4,php5,thtml,ctp"
  • Now go to your "Document and Settings" folder (found under your username e.g. "C:\Documents and Settings\Your User Name\Application Data\Adobe\Dreamweaver 9\Configuration"
  • Edit "Extensions.txt" and add the same lines you added to the other extensions file.

Using CakePHP with Aptana


  • From the Window menu, select Preferences..., and then choose General > Editors > File Associations.
  • Add the appropriate file type.
  • Next to the File Types list, click the Add button.
  • In the New File Type pop-up window, type the appropriate file extension (e.g. ".ctp" or ".thtml").
  • Click OK to add the New File Type to the List.
  • Associate the new file type with Aptana.
  • On the File Types list, select the file type that you just added.
  • Next to the Editor Associations list, click the Add button.
  • On the Editor Selection pop-up window, select the editor that you want to associate with your file type.
  • Click OK to add the editor.
  • The new is now associated with the specified file type.
  • Click OK to apply your changes and close the Preferences window.

CakePHP with gEdit or other Linux editors


Edit the /etc/mime.types file to point .ctp or .thtml files to the relevant editor you are using.

Debugging with CakePHP

02:41 , In , ,

CakePHP comes with some handy debug functions to make your coding life easier.

The first thing you should do is make sure Configure::read('debug') is set to 1 or 2. This will then allow use of debugger functions and also show database queries at the bottom of the page (very handy for profiling and finding bottlenecks).

print_r convenience function pr()

This function puts [pre] tags around the standard print_r to show the contents of an array in a well-formatted manner, making it easier to read and thus quicker to debug.
 function pr($var) {
if (Configure::read() > 0) {
echo '<pre>';
print_r($var);
echo '</pre>';
}
}
To find out information about your database table use pr($this->ModelName->schema())

Why doesn't the pr() function work?

What is your debug level set to? If it's less than 1, it won't display.

debug($var, $showHTML = false, $showFrom = true)

The debug() function is a globally available function that works similarly to the PHP function print_r(). The debug() function allows you to show the contents of a variable in a number of different ways. First, if you'd like data to be shown in an HTML-friendly way, set the second parameter to true. The function also prints out the line and file it is originating from by default.

Output from this function is only shown if the core debug variable has been set to a value greater than 0.

Logging with cakephp

Logging your output can be useful in a number of scenarios - especially in production servers where the debug level must be set to 0. To start logging output, use the log() function.

The log() function takes two parameters. The first is the message you'd like written to the log file and the second is used to define the log type you wish to write the message to. If not supplied, it defaults to LOG_ERROR, which writes to the error log previously mentioned. You can set this second parameter to LOG_DEBUG to write your messages to an alternate debug log found at app/tmp/logs/debug.log:

CakePHP Install Checklist (Apache)

02:03 , In

CakePHP is easy to install - extract the latest version into your web-accessible folder and you're good to go. If you don't see the default Cake page, try this checklist to see if you meet the necessary requirements for installing CakePHP on a Linux server.

  • Make sure you have the lastest stable release by going to http://cakeforge.org/projects/cakephp. You can download the nightly release from their SVN repository at https://svn.cakephp.org/repo/trunk/cake/ but take care to note this is not marked as stable for production.
  • Make sure that an .htaccess override is allowed: in your httpd.conf, you should have a section that defines a section for each Directory on your server. Make sure the AllowOverride is set to All for the correct Directory.
  • Make sure the .htaccess files are present - your operating system may treat these as hidden files and ignore them. These are essential.
  • Ensure mod-rewrite is enabled. This allows for "pretty" URLS such as www.mysite.com/posts/latest-bilge rather than /posts.php?post_id=934789. To enable mod-rewrite on Ubuntu Linux use: sudo a2enmod rewrite
  • What directory are you running the install from? If it's from a user directory (and not the webroot) then you need to set a RewriteBase [folder name] in the root .htaccess file.
  • If you are getting infinite redirect loop errors in your browser, this will probably be a rewrite problem. However this could also be caused by ACL permission issues - by default this is off so you should know if you've got ACL enabled for your app.
  • Are your sessions not working? Are you using a custom session handler? How are you saving your sessions? By default, CakePHP will use PHP's default session handler, if for some reason you don't have permissions to write to disk, you may get an error such as "session_start() [function.session-start]: open(/var/php_sessions/
    sess_55725da39073051a9529a124c99b54df, O_RDWR) failed: No such file or
    directory (2) [CORE/cake/libs/session.php, line 539]"
  • In this instance, make sure the /var directory is writable by the apache process.
  • What version of PHP are you running? You will need version 4.3.2 or higher (CakePHP runs equally well on 4 and 5 - though there may be different bugs in each)

Installing CakePHP On IIS

See this article for detailed instructions on how to install Cake on IIS.

Installing CakePHP On Cpanel

See this article for detailed instructions on how to install Cake on Cpanel.