Sunday, August 29, 2010

How to Open a File (xls, doc, pdf etc) through a link in CakePHP

Well this question was discussed in Google Group.
The poster wants to open xls, doc, or pdf files using CakePHP. As such, he wants to create a link, which when clicked, should open the document.

1. Save the file under /app/webroot/ folder. You can FTP that file for the sake of simplicity. If you need to upload files frequently, you can consider CAKEPHP file upload component. This component allows you to upload files from your computer to your server. Tweak it a bit to upload almost any file. But be sure to enforce all reasonable protections to prevent spammy uploads. Check to ensure that the file has been uploaded by pointing your browser to Next we need to do something in the respective view file.

2. Create a variable name $file_path in the respective view files.

Say,  $file_path='my_file_name.doc';
Now simply create a link to that path.

Saturday, August 21, 2010

Something on CakePHP Notice (8): Undefined Index

I do not know what should be the perfect title for this post - amazing cake or uncanny cake. But unfortunately, CakePHP can literally drive you CRAZY at times. So, we need to develop a methodical debugging algorithm for cake.


1. You are using proper Model/ Controller/ View setup.
2. You are firing correct SQL statement to fetch records. And database has at least one matching record.
3. Everything is just fine to the best of your knowledge and yet you don't really see any output in your view file.

To debug:

1. Set debug level to '2'.
As such my localhost is configured in that format by default. So, I do not have to change that setting too often. 

2. Read the output in the debug mode very carefully. Here lies the clue.
If you see a message like: 
Notice (8): Undefined index .. blah blah blah
CakePHP points out exactly what went wrong with the filename and line number even. This output is very reliable. "Undefined index" means you have used a variable somewhere but it has not been assigned any value. In production mode, CakePHP will ignore this warning and you will see nothing in your webpage leading to complete confusion.

3. You are more likely to fix the problem by correcting small typos in that specified file. Anyway, if you can't find anything wrong, follow the next few steps.

Check the value of the fetched recordset using debug($data) in your controller/action.
If you can see the values as per expectation, move on to your view files.
If you can't see any result, re-frame your find() conditions, examine your controller/action statements very carefully.

4. Check for typos.
Your variable names must be the same in controller set up and view files. Variable names you might have used in views without setting its value in the controller first. And remember the auto magic cake feature, which automatically converts controller variable names like:

So, in your view file, you need to mention myFunnyVars and not my_funny_vars.
I wasted more than few hours to fix that.

5. Few other tips I have found elsewhere:
When using scaffolding the order your associations must be the same as the order of the keys in your table to avoid getting this error message. Thankfully, I did not face that problem till now.

6. Have confidence in your cake knowledge. Keep your cool. And move methodically (check for view files, then controller setup, and finally your great models). If you can see desired result using debug($data) in your controller, problem is really trivial. To get into the details, you can use debug($data) in your view file to trace out the contents of the array elements as well.

Sounds too complicated? That's how CakePHP goes on.
Hope it helps someone, who might have been stumbling with Notice (8): Undefined index!!!

Saturday, August 7, 2010

How to use SEO urls in CakePHP without the ID

CakePHP offers massive support to create SEO friendly URLs on the fly. You can create SEO-friendly-urls without an ID value to refer to specific post id. It works like a charm like many other Cake MAGIC!

I'll refer to the post table.
1. Create a field called 'slug' (must be UNIQUE and NOT NULL) in the post table.

2. Download and use sluggable behavior to create post slug! Follow instructions step-by-step. It works perfectly.

3. Define routing rules in config/router.php file to display SEO friendly urls.




               'controller' => 'posts',

               'action' => 'view'



               'slug' => '[-a-z0-9]+',

               'pass' => array('slug')



4. In posts_controller.php view() function modify query,

function view($slug = null)
        if (empty($slug))
                // redirect ...
         $data = $this->Post->findBySlug($slug) ; 

You you should be able to access urls of the form:

Hope it helps someone.