Session Error in Cake App

I have spent the past 24 hours pulling my hair out over a very odd error in a CakePHP application I have been developing.
I had an app called training, so my directory structure looked like this (the training app was generated using bake)

Dreamweaver Screenshot

I had copied large amounts of code from another cake site that I am also developing including a simple login system to password protect the admin routes.

I noticed that as I was browsing the site and tried to login, I would login successfully into the admin area and then as soon as I tried to access any controller method in the admin section I would be logged out.

I echoed all my POST and SESSION variables to the screen in the layout using:

   
   

So I knew that I was being logged in successfully.

Session Screenshot

When I tried to access another controller action e.g. /admin/courses/index I would be logged out and asked to login again. Eventually I noticed that when I logged in I was being redirected to /training/admin/homes/index instead of the expected /admin/homes/index which is why I was being logged out.

I examined all the releveant files in my working application and my broken one and discovered that the only difference was that the working application was called /app/ and the broken one was called /training/ I re-named training to /app/ uploaded the site and it all worked again.

I am not sure at the moment whether this is the result of something I have misunderstood or if it is a bug, anyway I’ll stick it on trac when I get the chance.

Getting Bake to Work

A really powerful feature of CakePHP is bake –  a script located at  /cake/scripts/bake.php – it is similar in function to the Ruby on Rails Scaffold application – it is a command line programme that can generate outline or scaffolding code to carry out simple CRUD operations, based solely on your database structure.

I have had quite a few problems getting Bake to work reliably, but persevere because it is worth it. I have been developing on a PC running XP Pro and Apache2Triad 2.0.55

On windows there seems to be a bit of an issue with paths and in order to do any baking on an existing project you need to specify the full paths.

[I use the Command Window Here  Power Toy for Win XP which makes it easy to get the command prompt to the right place.]

To create a new cake application

  1. Navigate to \cake\scripts\ and open the command prompt.
  2. php bake.php -app name_of_application
  3. follow the onscreen instructions…

For Example: To create a new application called Flamingo:

php bake.php -app flamingo

bake

As you can see bake asks you if its ok to create a skeleton application on the path specified.

How to bake inside an existing application

If you want to bake inside an existing application, from the Cake tutorials and articles, it appears that you should be able to just type: php bake.php -app flamingo again, unfortunately this doesn’t work. Instead you get the following message.

how not to bake

What you actually need to type is something that includes the full path to your new cake application, something like:

php bake.php -app D:\apache2triad\htdocs\ctest\public_html\flamingo

If you already have a database structure you will see something like this:

Let’s bake

If you don’t yet have a database setup you will be prompted for connection details like this:

bake - no database

Happy Baking

A to Z links with CakePHP

On a lot of websites, especially directories or those which contain large amounts of alphabetical data you see A to Z lists. You know the idea, click on a A and see every entry beginning with A, click on B… etc.A to Z screenshot

then…

A to Z list

Well, here is how to do it with Cake (a new PHP application development framework). It isn’t at all hard.

First some prerequisites:

  1. A working cake setup.
  2. A simple model storing some data in the corresponding database table – in our case called Gallery.

CREATE TABLE `gallery` (

`id` int( 10 ) unsigned NOT NULL AUTO_INCREMENT ,
`title` varchar( 50 ) default NULL ,
`body` text,
`created` datetime default NULL ,
`modified` datetime default NULL ,
PRIMARY KEY ( `id` )

)

Step 1 – edit the gallery controller

You need to a new function to your gallery controller in order to show the A to Z link list. It should look something like this:

function show($str = null ) {  if(!$str)   

   {   

    $this->set('str','');   

   }   

  else   

   {   

    $this->Gallery->recursive = 0;   

    $this->set('galleries', $this->Gallery-> 
                     findAll("LEFT(title, 1) = '$str'"));   

    $this->set('str',$str);   

   }   

     

 }

Step 2 – create the view

You now need to create a view for this action, your view will should be called show.thtml and should be created in app\views\galleries. This is pretty simple – the only thing to take note of is right at the top – renderElement – rather than put the code to write the A to Z list directly in the view, I’ve decided to create it in an element. This means that it can be reused in any view within the site.

The first parameter ‘atoz‘ is the name of the element which in this case will be called atoz.thtml the second parameter is an array containing the varibales that will be available to the element – url is the action we want the links to point to, str is the value of str we selected e.g. b

<?php   

   echo $this->renderElement('atoz', array('url' => 
'/galleries/show/', 'str' => $str));   

?>
<?php
if(!empty($str))   

 {
?>   

<table cellpadding="0" cellspacing="0">
<?php foreach ($galleries as $gallery): ?>   

<tr>   

    

 <td><?php echo $gallery['Gallery']['title']; ?></td>   

 <td><?php echo $gallery['Gallery']['body']; ?></td>   

    

 <td nowrap>   

  <?php echo $html->link('View', 
      '/galleries/view/' . $gallery['Gallery']['id'])?>   

 </td>   

</tr>   

<?php endforeach; ?>   

</table>   

<?php
 }
?>
 

Step 3 – create the element

<?php
 $alphabet = array('a','b','c','d','e','f','g','h','i','j', 
'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'); 
  
 echo('<ul class="atozlist">'); 
 $i = 1; 
 foreach($alphabet as $link) 
  { 
   $class = ''; 
   if($i == 1) 
    { 
     $class = ' class="firstitem" '; 
     $i++; 
    } 
     
   if(strtolower($str) == $link) 
    { 
      echo('<li ' . $class . '> 
           <span class="thisitem">' . $link . '</span></li>'); 
    } 
   else 
    { 
     echo('<li ' . $class . '><a href="' . $url . $link . '">
        ' . $link . '</a></li>'); 
    } 
  } 
 echo('</ul>'); 
?>

Cook up Web sites fast with CakePHP

I was reading the Google Group for Cake PHP today, one of the posts mentioned a link to a really good set of tutorials about Cake, which I have been reading this afternoon. You need to register for an IBM id but it definately worth it – I wish I had found out about the articles a few weeks ago.

http://www-128.ibm.com/developerworks/edu/os-dw-os-php-cake1.html

So far I’ve only read the first 3 articles but the it is very well writen, particularly the information about ACLs.