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.