Full CakePHP 1.2 App Part 12
OK so finally the full CakePHP application is coming to a close and this article is just going to wrap up a few loose ends. I've noticed a few errors and layout issues that I'm going to fix and hopefully by the end of the article the app will be fully complete.
Like all applications you could probably keep going forever adding new features, tweaking code and cleaning things up but there must come a time when you have to release it and the close the door. After this I probably wont be adding more features for some time but I do have a few ideas about great features you could try and implement.
The application can be found online at cakecatalog.jamesfairhurst.co.uk with a few more DVDs added.
Genre Filter Problem
When filtering DVDs by Genre there is a display error with only a few DVD covers being displayed per shelf. On further inspection the problem occurs because when the Genre filter is active I have to manually remove the DVDs that don't match. To display 8 DVDs per shelf I use the index of the array to determine the current position and because I've manually removed DVDs from the array the index has been messed up.
To fix this I've added a simple counter in the /app/views/dvds/index.ctp so instead of using the array key index I'm going to use the counter instead. The changed lines are below to you just need to do a quick copy and paste.
// file: app/views/dvds/index.ctp <?php // check $dvds variable exists and is not empty if(isset($dvds) && !empty($dvds)) : // init dvd count $count = 1; ?> <div class="shelf"> <?php foreach($dvds as $key=>$dvd): ?> <?php // calculate if this dvd is the last on the shelf // if dvd number can be divided by 8 with no remainders $last_dvd = ( (($count) % 8 == 0)? 'dvd-last' : '' ); ?>
Image Upload Problem
When uploading images that already exist there are spaces in the new name that throws up some validation errors when viewing. This is a simple fix by removing the spaces from the new filename like so:
// file: app/app_controller.php // from $url = $rel_url.'/'.$now.' - '.$filename; // to $url = $rel_url.'/'.$now.'-'.$filename;
Integrating the new Image Resize Class
Recently I created an Image Resize class that will resize images on the fly and save a cached version on disk so save processing time next time the image is requested.
I copied the images_controller.php file from that post and pasted it into the controllers directory. I also created a new cache folder located at /app/webroot/img/cache to hold all the processed images.
Below is a quick change to the index.ctp so utilise the Image Resize Class. I wont go through all the steps here to get up and running with is so check out my previous article if your interested.
// file: app/views/dvds/index.ctp <!-- <img src="/<?php echo $dvd['Dvd']['image']; ?>" alt="DVD Image: <?php echo $dvd['Dvd']['name'] ?>" width="100" height="150" /> --> <img src="/images/view/100/150/true/<?php echo $dvd['Dvd']['image']; ?>" alt="DVD Image: <?php echo $dvd['Dvd']['name'] ?>" width="100" height="150" />
Single DVD View
The view single DVD page is quite simple and will display the full details of the DVD along with the cover image. Not much has changed in the actual view.ctp file except that a new layout has been created and the page has been styled with CSS.
Single Genre View
The view single Genre page displays the DVD covers on the shelf like the main index page and includes a title to display what Genre is being displayed. The new layout has been used so that the header can be displayed without the form.
Source Code
The source code for this article can be downloaded using this link. If these articles are helping you out why not consider donating I can always use a beer! :)
Wrapping Up
Wow I've finally managed to wrap up most of the loose ends and the application is looking fairly complete, looking back I didn't think so time and effort would have gone into building an application like this but documenting every step is quite time consuming but hopefully its all been worth it.
Here a are few proposed improvements and features that I've not got round to implementing but they may get done in the future:
- Template system to change the look of the front end from the admin panel
- Amazon Lookup: When inputting a DVD automatically retreive data from Amazon
- A sprinkle of Ajax to make everything run a little more smoothly
Comments
user (05/09/2008 - 02:27)
hi, is there a full source code available for download?
James (06/09/2008 - 01:49)
The full source code has just been added.
Gerald (06/09/2008 - 21:26)
Hi!
I found this tutorial very interesting and wonderful specially for cakePHP beginners. One thing I wish you could have time considering too... that is, allowing the site visitor to search DVDs via Genre.
When I tried visiting the demo page of this tutorial series and tried the search form entering an existing genre as keyword:
"There are currently no DVDs in the database."
That's all I have in mind. Again, thanks for making this great tutorial!
user (07/09/2008 - 05:00)
thank You! :)
James (07/09/2008 - 10:11)
@Gerald: Thanks, I've not included a search via Genre because you can filter by Genre and then search through the DVDs that were found. Although adding a Genre search wouldn't be too much of a problem.
Neil Crookes (07/09/2008 - 12:07)
Hi James,
I just saw a mention of your site and your work with cake on <a href="http://cakebaker.42dh.com/2008/09/07/building-a-dvd-catalog-application-with-cakephp/">Daniel Hofstetter's blog</a> and thought I'd come along and check out a fellow UK baker's site. It looks good mate, so good I want to add your RSS feed to my reader - but I can't find your feed anywhere! Why don't you check out cake's rss helper and do a tutorial on it too? Keep up the good work.
Neil.
James (08/09/2008 - 02:47)
@Neil: Thanks for getting in touch, the RSS feed for the site should be available from your browser but it would be a good idea to do an article on it so that might be in the pipeline.
Kiran Aghor (18/09/2008 - 21:21)
I learnt a lot from this series.
-- creating admin section of the site
-- authentication
-- layouts, elements
-- adding a list of genres and storing them to the db(this technique was kool :)
-- uploading files with validations (great one!)
Thanx a million for this series. Please keep them coming when u get new ideas, techniques in cakephp.
James (19/09/2008 - 03:16)
@Kiran: Hey thanks for getting in touch and I'm glad the series helped you out.
Chirayu (26/09/2008 - 05:37)
Hey James. certainly excellent work. I copied source folder and restore database. working good, only thing i am not getting images in front and admin as well. but if i use absolute url , its fetching . any guess for that ?
James (27/09/2008 - 08:27)
@Chirayu: Thanks for commenting, hmmm not sure why the images aren't working. Have you tried adding a few Dvd's to the system?
Willem (09/10/2008 - 16:24)
Thanks for this great Tutorial!
Is there are pdf version planned? If not, is it okay to make one myself and give it to my friends?
Sorry for my bad english, I'm from germany and it's 1:23 ^^
James (10/10/2008 - 05:19)
@Willem: Hey thanks for commenting, a PDF is a great idea and your more than welcome to make one and send to friends. I might start compiling my own version for release on the site if I've got the time.
ProLoser (02/11/2008 - 17:15)
Hey I was looking at your blog and you seemed to have accomplished something I'm having one horrible hell of a time getting past: filtering by HABTM relationships for my search results. Especially for more than 1 filter. I want to know how you did it and I can't seem to locate the source code you said was available. Any help appreciated, thanks.
James (03/11/2008 - 13:46)
@ProLoser: Source code is at the bottom of every post in a zip file for the CakeCatalog, filtering by HABTM is quite tricky as you cant really write a custom sql statement to filter results, what I had to do is grab all the results and filter them manually in the Controller. If you check out the main dvds_controller.php you should be able to see whats going on.
cristou (08/01/2009 - 03:16)
Ola James
i would thank you for your great and excellent work!
im just begin to learn Cake, and it's help a lot!
keep it up
have good time
thx
Baiju (19/01/2009 - 23:52)
Hey James,Nice Work..Thanks for posting your code...I downloaded the source file..but did not find sql file in it...
thnx
have a great day!
James (20/01/2009 - 02:28)
@Baiju: thanks for commenting, didn't realise the sql wasn't in this zip file. In the meantime download the source code from the first article and the sql file should be in that one.
Joćo Batista Ladchuk (08/03/2009 - 19:45)
Congratulations, I'm looking for a few months ago as his example! Finally found. Sorry my bad English, but I am Brazilian and here there is very little material on cakePhp. His examples are helping me a lot. Thank you. Have estimates of when a page will show the registration of users?
James (21/03/2009 - 06:14)
@João: Thanks for commenting and sorry for the late reply. I'm glad these are helping you out, in regards to the user registration you should be competent enough to code something like that up yourself, and if not the learning process will be invaluable for you. Good luck!
Zoran (28/03/2009 - 11:11)
Hi James.
Thanks for this tutorial, it helped me a lot. I am new cakePHP from Macedonia. I was wondering if i could have multiple groups of users like admins, managers and users.
Could i have in the core.php like this:
Configure::write('Routing.admin', 'admin');
Configure::write('Routing.manager', 'manager');
And then in the controllers to have admin_add(), admin_delete(), manager_add(), manager_delete()?
Also, could i use MooTools instead of Jquery with CakePHP?
Thanks in advance
Zoran
James (29/03/2009 - 13:53)
@Zoran - glad you found the "multiple admins" article and yes you can use MooTools instead of jQuery, just put the library in the webroot and include as normal.
Nat (07/04/2009 - 10:45)
Hi James,
This is an extremely helpful tutorial, it's given me more of a handle on CakePHP than my textbook on the subject - just having comments in the code makes a huge difference, as does having the source code to compare against when things don't work. I also think it's a much more helpful example than yet another blog.
A couple more bugs though:
1, Deleted genres are appearing in the Top Genres list (I only have 5 genres in my db, 1 deleted so this is easy to spot).
2, Images are being written in the views as IMG tags with /images/ or /img/ relative to root - this is bad practice, if the application were accessed via http://localhost/catalog/ the images would be broken. If you simply use the HTML helper to draw the image tag it'll automatically get the URL right regardless of if you're using the virtual host or not.
3, When I view resized images directly in my browser, they still appear to be full sized. They're only coming out smaller due to the width and height tags - in fact in the /dvds/view/* view the image is drawn fullsized as there's no width and height at the end of the image tag.
All minor problems, and I always think there should be a little something 'left as an exercise to the reader' :)
Thanks again! There should be some virtual coins landing in your tip jar about now, have a pint on me!
Nat.
James (07/04/2009 - 13:38)
@Nat: Hey thanks for commenting and I'm really glad the tutorials helped you out. Thanks for the feedback as well, all good points and definitely something to look into.
V (26/04/2009 - 01:57)
Complete beginner, LOVED your series (it actually just saved my life as I have a near death experie err.. VERY SHORT deadline).
Do you plan to add some ajax tutorials (using jQuery) for adding (ie) types on the fly from admin/dvds/add ?
Thanks a million !
V.
James (26/04/2009 - 08:50)
@V: Hey thanks for getting in touch and I'm glad the articles helped you out. Good idea about the jQuery tutorial I'll have to rustle something up.
ansh (11/05/2009 - 15:50)
Nice One!!! Can u also add one more post to add Ajax features in this app. ?
James (18/05/2009 - 23:54)
@ansh: Hi there and thanks for commenting. I'm really quite busy at the moment so I don't think I'll have time to add Ajax functionality to the app but I'm sure it wouldn't be too difficult and would be a great learning experience for you.