The site we have recently completed for a new client was built using the rapid development framework CakePHP. While there is an initial learning curve with the naming conventions and methods, once you get them right it makes quickly developing editable web sites a much more pleasant experience. We recommend checking it out. And if you need systems built using this kind of framework, contact us.
Here’s a useful and simple method we learned to add a nice little year / month archive list to the blog we built for the client’s site, using standard cakePHP methods it was easy. If you are new to this, first, we recommend going through the tutorials and documentation on the cakePHP site, as they will familiarise you with the terms cake uses, MVC architecture in general and are an excellent resource to get you started.
So, assuming you already have your blog in place, perhaps from the tutorial in the documentation, open the controller file for it, probably named blogs_controller.php in the app/controllers directory, and find the index function for the blog. Inside that function, typically after any recursion setting for the blog (in this example we set it to 0), and before the line where you request your list of blogs, which will probably read like;
$this->set('blogs', $this->paginate());
You add the following set of code;
$archives = $this->Blog->find('all',
array(
'fields'=>array('DATE_FORMAT(Blog.modified, \'%Y %M\') AS dd','count(Blog.id) AS numblogs'),
'order'=>array('dd DESC'),
'group'=>array('dd')
));
$this->set(compact('archives'));
This gets us a list of the blogs, grouped by year and month format in descending order, plus a count of the number of blogs in that group for good measure and a an assist for the front end experience.
Now, in our view file, index.ctp, which you’ll probably find in the app/views/blogs directory, find the location you want your archive list to appear and add in the following code;
<?php
$curr_year = '';
foreach ($archives as $archive):
if ($curr_year != substr($archive[0]['dd'],0,4)) {
if ($curr_year != '') {
echo '</ul>';
echo '</li>';
}
echo '<li>'.substr($archive[0]['dd'],0,4);
echo '<ul>';
}
echo $html->tag('li',$html->link(substr($archive[0]['dd'],4).' ('.$archive[0]['numblogs'].')', array('controller'=>'Blogs', 'action'=>'index', 'date'=>str_replace(' ', '-', $archive[0]['dd']))));
$curr_year = substr($archive[0]['dd'],0,4);
endforeach;
?>
Now all you need to do is open your blog index page from your browser and you should have a nicely formatted archive index, which you can style to your hearts content, with a total of that months blogs next to each month that has any.
We hope this was useful, more cakePHP tips soon!