Easy CakePHP in Ubuntu

I’ve always setup a new CakePHP project by extracting the current stable build into a project directory and editing the core.php and database.php files manually. I usually then change to the /cake/console directory of the project and run php cake.php bake (having already installed the php-cli) to bake my code.

I know there are other ways to make this process simpler but its always just worked for me and I am usually to lazy/busy/unmotivated to try something new.

Today, however, I stumbled across a great way to set up CakePHP in Ubuntu so that building new projects is a simple matter of running one line from the command line and you are ensured of using the same stable “shell” version of Cake each time. It is also extremely easy to upgrade your “shell” version whenever you want. (It also makes baking even easier than the way I was doing it but that’s just an added bonus !)

(This may or may not also be possible in other Ubuntu like systems that have the same CakePHP repositories eg Mint, Debian etc)

1) Download and extract the latest stable version of CakePHP to a folder on your machine.

2) Install cakephp-scripts from the Ubuntu repositories

$ sudo apt-get install cakephp-scripts

This will install the current version of CakePHP in the Ubuntu repository, as well as the php-cli (command line interface) and setup the Cake console for easy use from the command line.

3) Copy the folders from the latest stable version to your system

Currently you have installed whatever version of cake was included in your version of Ubuntu’s repositories. If you are running an older version of Ubuntu or just want to run the very latest stable version, you need to update your Cake version by copying it from the extracted folder in step 1.

The “shell” version of CakePHP you installed in step 2 is located in /usr/share/php/cake. You will notice that the directory structure is slightly different from the extracted version, but a little common sense will enable you to see what needs to be copied to where (basically, from the extracted directory, copy the app directory and everything from inside the cake directory into the /usr/share/php/cake directory).

You can also upgrade your “shell” by downloading/extracting a newer stable version of CakePHP and repeating the above step.

Now baking with Cake becomes even easier.

Change to your web root (usually /var/www/ or wherever you usually develop your PHP applications) and run

$ cake bake projectname

where projectname is the name of a new project you want to create. You will be presented with the Cake console, which will set up your project folder and copy over all the code from the CakePHP “shell”. It will also set up a random Security Salt value, add some default CAKE_CORE_INCLUDE_PATHs and guide you through setting up your database configuration(s).

You can then change directory to your projectname directory and run

$ cake bake

to bake your Models, Views and Controllers.

Happy Baking…

UPDATE: I just noticed another nice side effect of this way of creating Cake projects, default app_controller.php, app_helper.php and app_model.php files are created in the project root folder. 😉

Developing with CakePHP: Creating a simple admin control panel

If you are like me, you prefer to have a specific url mapped to your applications control panel/backend etc.
This is usually something along the lines of www.url.com/admin or www.url.com/cms.

Normally during the CakePHP “bake” process, the system will ask you what you want your admin route to be (default “admin”). Once this has been set you are merely 3 simple steps away from creating your own simple user control panel….

Step 1) Routing the admin url.
Add the following to your app/config/routes.php

Router::connect(‘/admin/’, array(‘controller’ => ‘admin’, ‘action’ => ‘index’));

This will route url.com/admin to the admin controller index function

Step 2) Creating the admin controller
Create the admin_controller.php file in /app/controllers/ and add the following code

class AdminController extends AppController
{
var $name = ‘Admin’;
var $uses = array(); //This controller does not need to use a model

function index(){

}
}

Step 3) Create your views
Create the app/views/admin/ directory and add an index.ctp view to it, with whaterver code you prefer (mine is merely a bunch of links to other controllers at this stage)

  • link(‘Posts’, ‘posts/’);
  • link(‘Comments’, ‘comments/’);

My first published article.

Something of a first for me. I recently started playing around with the CakePHP framework again. I have had some (little) experience with it in the past and with the launch of two CakePHP books I decided it was high time I learned how to use this awesome framework properly.

A few days later I was trying to find a way to integrate my favourite html editor into the framework itself and after a lot of googling, trial and error and a little luck I managed to find a simple, reusable way to integrate the editor into the framework. As this was very helpful to me I posted an article on the CakePHP bakery, and the chaps over there deemed it worthy enough to be published.

You can read it here . Please feel free to send me your comments.

I’m just kinda chuffed the published it. 😉