CakePHP Helper To Generate Two Different Link For Canvas APP And Web App Depending On signed_request

I am developing a Facebook App for Canvas and Web. When a user comes to canvas app all of the link of app will start with “https://apps.facebook.com/appname/” and when users comes web app all link start with the web url. This impotant for user expericne in canvas app. As app the canvas apps are iframe based, it is possible that user might go to a page and then refresh the browser. So browser will point to start page. Suppose A user comes to “https://apps.facebook.com/appname/profile.php” to view his details in the app. On that profile page the is link that points to home.php. When user clicks that link use redirected to home.php but the browser address bar points to “https://apps.facebook.com/appname/profile.php”. Now if the user refreshes the browser he/she will be redirected to profile.php.

In canvas app Facebook will always send “signed_request”. So we can easy check if a user viewing our canvas app or web app.

In CakePHP developing helper is very easy. You can still use core helper in your helper. user So let’s develop a helper to solve our problem. So first create a file “link.php” in “/app/view/helpers/” folder. This helper will use a core helper “Html” helper to be precious.

class LinkHelper extends AppHelper {
	var $helpers = array('Html'); 
}

We will have two function one will generate links and other will generate urls. Let’s do the link one first.This function will have same paramet as core link function has. The function will check $this->params[‘form’][‘signed_request’] for signed_request sent from Facebook server.

function customize_link($title, $url = null, $options = array(), $confirmMessage = false ) {
    if(isset($this->params['form']['signed_request']) and $this->params['form']['signed_request'] !='')
    {
        $url = $this->Html->url($url, false);
        $url = 'https://apps.facebook.com/appname'.$url;
        return $this->Html->link($title, $url, $options, $confirmMessage);
    }
    else
    {
            return $this->Html->link($title, $url, $options, $confirmMessage);
    }
}

To use this function you have add the helper in the controller. I always add all my helper in app_controller.php. So that all the helper will be available to all the controllers. To use this function use the following example

echo $this->Link->customize_link("Click Here ", array( 'controller'=>'users', 'action' => 'index'), array("target"=>"_top"));

echo $this->Link->customize_link("Click Here", '/users/index', array("target"=>"_top", 'title'=>'Webinars'));

The function will generate url like http://website.com/ or https://apps.facebook.com/appname/ depending on signed_request

function customize_url($url = null) {   
    if(isset($this->params['form']['signed_request']) and $this->params['form']['signed_request'] !='')
    {
        $url = str_replace('development', '', $this->Html->url($url, false));
        $url = 'https://apps.facebook.com/appname'.$url;
        return $url;
    }
    else
    {
            $url = $this->Html->url($url, true);
            return $url;
    }
}

To use this function use the following examples

echo $this->Link->customize_url(array( 'controller'=>'users', 'action' => 'index'));

echo $this->Link->customize_url('/users/index');

Here is Helper file to download Click Here To Download.

About Zakir Hyder

This entry was written by .

27. June 2012 by Zakir Hyder
Categories: CakePHP, Facebook | Tags: , , , , , , | Comments

Comments

  1. […] CakePHP Helper To Generate Two Different Link For Canvas APP And Web App Depending On signed_request I am developing a Facebook App for Canvas and Web. When a user comes to canvas app all of the link of app will start with “https://apps.facebook.com/appname/” and when users comes web app all link start with the web url. This impotant for user expericne in canvas app. As app the canvas apps are iframe […] […]