Difficulties, CakePHP and the Facebook API

Required knowledge: CakePHP, PHP, Facebook API

Trying to integrate CakePHP and the Facebook API has given me a headache for the past couple days. First off I didn’t know where to start besides dropping the Facebook php files into my vendors directory.

Luckily I found a very useful post here. It goes on to explain how to set up your app_controller.php and a sample Controller and View using Facebook’s FBML. If you’re looking to start a Facebook application in CakePHP this is the place to start.

Later on I started having problems with posted data. Whenever I had a canvas page that handled posted data and then displayed something depending on what was posted I’d lose the post while redirecting. The following code for example:

function add(){
  $output = "";
  if(!empty($this->data) ){
    if($this->Team->save($this->data)){
      $output = "success";
    }
    else{
      $output = "error";
    }
  }
  $this->set("output", $output);
  $this->facebook->require_frame();
}

This code would save the data but $output would still be blank. The reason being is that after the redirect during require_frame(), $this->data is lost. So instead of making pages like the one above I would do the following:

function add(){
  if(!empty($this->data) ){
    if($this->Team->save($this->data)){
      $this->redirect('/teams/success');
    }
    else{
      $this->redirect('/teams/invalid');
    }
  }
}

This way I redirect completely without calling the require_frame() method until I am in either “/teams/success” or “/teams/invalid”.

I’ll post more as I get further into it.

3 Comments »

  1. Matt Huggins says:

    I use require_frame() in CakePHP without needing to worry about all this. I simply place the call to require_frame() prior to any other processing (once the Facebook object is created, that is). You can easily achieve this by placing it in your beforeFilter function of either app_controller.php or your current controller.

  2. thaichaiguy says:

    Thanks for the comment Matt! I swear I tried putting the require_frame() at the beginning of my function with the same results. That would be the same as putting it in the beforeFilter right?

    But since you’re the expert I’ll try it again.

  3. Matt Huggins says:

    Your comment got me to thinking that you might be setting something up in your app differently than expected. Are you setting up your anchor tags (links) within your app as “http://apps.facebook.com/appname/page.php”, or are you setting them up as “http://www.callbackdomain.com/appname/page.php”? If you’re setting them up as the latter, try changing all your links to the former before using require_frame() as I described above.

    As with your app links, your form action URL’s should also be set up to point within the app.facebook.com domain, not your callback domain. (There are some special cases where you might want to point directly at your callback domain, but typically this is not how it should be done.)

Leave a Reply

Login