Starting CakePHP from scratch (sorta)

I have had several attempts at setting up a cakePHP project (well several projects, actually) because I never quite get enough time to read the documentation, follow the tutorials or actually design what I want to do.
This time will be different – well partly – because I want to go back to the starting block and build what will be a production workflow system.

These are my random jottings.

First off: make sure that the infrastructure is correctly installed.
I am using

Since these are running on a windows XP box at home, it will be interesting to see what happens when I move it to my production server which runs on a linux box.  Most (but not all) settings look the same or similar enough.

Lesson 1

Check that you have a recent version of the pcre (regular expression library)! For some reason, I had a very old library on one of my dev machines and some things broke!

Getting started

You may want to have a look at PHP MySQL Tutorial
Download the appropriate binaries/zip files.
Install (in no particular order?) apache, php and mysql (and the mysql tools) and make sure they are correctly configured.
I generally create a phpinfo.php file and stick it in the web root directory just to help me check things out.
<?php
phpInfo();
?>

Nothing to it really, but at least it shows that php and apache are behaving nicely.

Checking mySQL is a little trickier.
Create a new database and a new user – mySQL Administrator is recommended here.
For this lets’ call the database catalog “test”, and the user “testuser”.
For now, give testuser all Schema Privileges for the test catalog.

Now create a table – assume the table is called “tests”:


CREATE TABLE test.tests (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(45) NOT NULL,
created DATETIME NOT NULL,
modified DATETIME NOT NULL,
PRIMARY KEY (id)
)
ENGINE = InnoDB;

The field names are not entirely random here!

Fire up the MySQL Query Browser and login as testuser specifying test as the default catalog and stick some data into the database.


INSERT INTO tests (name, created, modified)
VALUES
('test 1', now(), now()),
('test 2', now(), now()),
('test 3', now(), now()),
('test 4', now(), now()),
('test 5', now(), now())

Create a file dbtest.php to check everything is working.

<?php
echo "
<h1>mySQL test</h1>
";
$dbhost = 'localhost'; // or whatever host your database is installed on
$dbuser = 'testuser';
$dbpass = 'test123';
$dbname = 'test';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (‘Error connecting to mysql’);
mysql_select_db($dbname);

$query = “SELECT id, name, modified FROM tests”;
$result = mysql_query($query);

echo ”
<table border=”0″>
<tbody>
<tr>
<th>ID</th>
<th>Name</th>
<th>Modified</th>
</tr>
“;

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo ”
<tr>
<td>” . $row[‘id’] . “</td>
<td>” . $row[‘name’] . “</td>
<td>” . $row[‘modified’] . “</td>
</tr>
“;
}

echo “</tbody></table>
“;
mysql_close($conn);

?>

Installing cakePHP

Assuming everything is working, now is the time to install cakePHP.
If you follow any of the other tutorials, they just unpack things into a single directory structure. However, I find this a problem because there are some files I need to create or modify, and lots that I should not change. Also, to make use of the console there are one or two things I need to do too.

First of all, need to upack the zipped cakePHP files remembering to use paths when you unzip! – I’ll do that to c:\tmp (I’m using windows – you may need to create this directory).

The cakePHP core files I’m going to put into c:\lib\cake, and I will put the application into c:\webapp\app

  1. Extract cake zip file to c:\tmp (using folders)
  2. Copy c:\tmp\cake to c:\lib\cake
  3. Copy c:\tmp\app to c:\webapp
  4. Configure apache to use c:\webapp\app as DocumentRoot directory (don’t forget permissions)
  5. Update c:\webapp\app\config\index.php
    1. Define the root directory:

      if (!defined('ROOT')) {
      define('ROOT', 'C:' . DS. 'webapp');
      }
    2. define the cake library directory:

      if (!defined('CAKE_CORE_INCLUDE_PATH')) {
      define('CAKE_CORE_INCLUDE_PATH', 'C:' . DS. 'lib' );
      }
  6. Check it out : http://localhost/
    It should fire up showing a couple of warnings (Security.salt value and database configuration).
  7. Edit c:\webapp\app\config\core.php to change the salt value:Configure::write('Security.salt', 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi');
    Replace the 40 character string with another 40 character string (random numbers, letters, punctuation such as ‘a{oihw£{oihnocvniOIQH[EF98IH{od:vin:{oh*’

    If you check outhttp://localhost/, it should remove that security warning.

  8. Copy c:\webapp\app\config\database.php.default to c:\webapp\app\config\database.php
  9. In the c:\webapp\app\config\database.php file, change

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

    to the values used in our test database

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

    If you check : http://localhost/ now, we should see all green lights!

Final checks

We are going to create a model and controller using the wonderful scaffolding feature just to check everything works.

  1. Create a model file for the tests in c:\webapp\app\models\test.php:


  2. Create a controller for the tests in c:\webapp\app\controllers\tests_controller.php:


  3. If you check : http://localhost/tests/ you should see the test data that you put into the database, and links to view edit and delete records.

Comments are closed.