Preparations for baking an application

Published on and tagged with cakephp  configuration

Before you can start with baking an application you have to do some preparations. Those preparations differ from developer to developer. So the following is just one possible way, my way, of doing those preparations. It is the way that works for me now, but of course it can and will change in the future as it has in the past. As this article is about preparations you have to do for each project I will omit things like the installation of CakePHP.

First a summary of the steps involved:

  • Create a repository for the project
  • Create a project in the IDE
  • Bake the application skeleton
  • Create a database
  • Rename database.php.default to database.php and modify it
  • Modify app/webroot/index.php This step is no longer necessary as it is done automatically by the bake script
  • Make hosts entry
  • Create virtual host

Create a repository for the project
I strongly recommend to use a version control system like CVS or Subversion for each project, even if you are the only developer. To the creation of a project repository itself I cannot say much, because I use the web-based tool provided by my hoster.

Create a project in the IDE
In my IDE, PHPEclipse PDT, I then check-out the empty project from the repository and create a PHP project.

Bake the application skeleton
After a switch to the command line I use the bake script to create the application skeleton for my project with:

dho@tumulux:~/projects/cake_1.2.x.x/cake/console$ ./cake bake project /home/dho/projects/projectxy/trunk/app

Create a database
As I am already on the command line I create the MySQL database for the project:

dho@tumulux:~$ mysql -u root
mysql> create database projectxy;
mysql> quit
dho@tumulux:~$ 

To make my life easier I use the root user without a password for all database-related activities on my development system (as you can guess it is a bad idea to use that approach on a live server *g*).

Rename database.php.default to database.php and modify it
To use the previously created database with CakePHP, two steps have to be performed. The first is to rename the file database.php.default in app/config to database.php. And the second step is to modify the database configuration:

var $default = array(
    'driver' => 'mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'root',
    'password' => '',
    'database' => 'projectxy',
    'prefix' => '');

Make hosts entry
As I want to have the development environment as similar as the live environment, I add an entry to /etc/hosts for each project:

127.0.0.1      projectxy.localhost        localhost

Create virtual host
As I use a production setup for development purposes, I define a virtual host in /etc/apache2/sites-available/default that looks like:

<VirtualHost *>
    ServerName projectxy.localhost:80
    DocumentRoot /home/dho/projects/projectxy/trunk/app/webroot
    
    ErrorLog /var/log/apache2/projectxy_error.log
    CustomLog /var/log/apache2/projectxy_access.log combined
</VirtualHost>

After this change I have to restart the Apache web server with

dho@tumulux:~$ apache2ctl restart

Finish
And voilà, if I request http://projectxy.localhost with my browser I get the default cake page saying the database configuration file is present and that cake can connect to the database. With that I am ready to start baking my application :)

Update (2008-08-28): Adapted to the current version of CakePHP 1.2

14 comments baked

  • Davide

    For apache setup, I prefer use a port addressing instead of NameVirtualHost. This because if you are behind a proxy and want to refer to projectxy.localhost, you should add it in the exclusion list from proxy of the browser. Instead if you refer to the application with something like localhost:9002 then the proxy exclusion is automatic and you can also skip the /etc/hosts file. Sure it’s less readable but there exists bookmarks :)

    here is a httpd.conf snippet


    Listen 9002

    NameVirtualHost *:80

    DocumentRoot “H:/www/tilug-devel”
    ServerName “localhost”

  • Leandro Ardissone

    Great Article!

  • Dr-Leech’s Blog » Preparando todo para cocinar el pastel

    […] Encontré este post en CakeBaker que explica como Daniel Hofstetter prepara todo el entorno para comenzar con a desarrollar una aplicación usando CakePHP. […]

  • cakebaker

    @Davide: Fortunately I do not have to use a proxy ;-) At least for me it is much easier to remember a name than a port number (and so I don’t need bookmarks for local stuff).

    @Leandro: Thanks :)

  • ryan

    i’m just getting into using SVN and PHPEclipse w/ my Cake apps..

    had a couple questions –

    Do you add all of your cake app directories to your repository? (config, vendors, etc)

    Also, do you strip out the SVN and other comments from ‘baked’ files such as app_controller and app_model?

    I think that’s clear, let me know if otherwise

    – Thanks

  • cakebaker

    @ryan: Yes, I add the entire app directory to the repository.

    I leave the comments in the files as I am too lazy to remove them ;-)

  • Tim 'DoughBoy'

    Great article. Thanks for the time and effort.

    After wrestling with the final stage for a couple of hours I managed to get this to work by adding the following just before the virtual host definition:

    NameVirtualHost localhost:80
    ServerName “localhost:80”
    DocumentRoot “C:\Program Files\xampp\htdocs”

    I would be happy for someone to tell me that there was a better way!!!

  • cakebaker

    @Tim: Thanks, I am glad it was useful for you.

    I looked at my Apache config file, and there is the following line before the virtual host definitions:

    NameVirtualHost *

  • Andrew

    Is it possible to set up cake such that multiple applications can run out of the same hostname, without needing a virtual server for each?

  • cakebaker

    @Andrew: I think it should be possible, but I don’t know how as I don’t have used such a configuration yet.

  • Peeter

    it would be much easier for testing and developing if one could easily configure cake apps to just one host subfolders ala

    mydomain.com/cake/project1
    mysomain.com/cake/project2

    saves you so much time and is lot easier to find and access different projects

  • cakebaker

    @Peeter: That should be possible. But personally I prefer to have an almost identical environment on my dev machine as on the live server.

  • ryan

    Peeter – that’s completely doable. Like cakebaker, I setup most of my projects w/ virtual hosts so my site runs locally as close to the live environment as possible; However, as long as you define the paths correctly in webroot/index.php, you can access your app from http://localhost/projectname/app/webroot – I do this when I need to quickly set something up for testing/experimenting. The only thing that gets screwed up when you do this are the image URL’s in the skeleton cake.generic.css; I believe they’re written from the root in the css file (/path/to/image.jpg) but you need to adjust this since http://localhost/ is your actual web root when accessing your app in this manor – i BELIEVE the fix for this is leaving off the initial root ‘/’, but I don’t recall off hand – sounds about right tho. This is a good thing to plan for with your stylesheets anyway, in case you need to move your site to a host w/ a funky directory structure or into a subdirectory or something.

  • cakebaker

    @ryan: Thanks for these additional explanations.

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License