If Only I Knew That!

Webby things that could save you time

Archive for August, 2013

Laravel 4 and Facebook PHP SDK

with 2 comments

After spending many hours trying to work out why I couldn’t get the ‘signed_request’ I found laravel 4 redirects to remove the trailing slash with the redirectIfTrailingSlash method. Facebook app URL’s have to have the trailing slash on the end or be a file i.e. ‘/’ or ‘index.php’

If you test your app and point it to the ‘/’ route the app works fine as the redirectIfTrailingSlash method doesn’t redirect. So how do you get a deeper route to work? Lets take a look at the route for ‘tab’, we have to use the post or any as Facebook posts the data to the route, you also need to include the trailing slash as you’ll get a 404 otherwise

The route needs to be

// I name space my controllers but you can see the important bit to define the route
Route::any('tab/', array('as' => 'tab.index', 'uses' => 'App\\Controllers\\TabController@anyIndex'));

Next we need to address the redirectIfTrailingSlash method, I do this by extending \Illuminate\Foundation\Application class and override the redirectIfTrailingSlash method to include a excludePaths in the condition, like so

There is probably a more dynamic way to do this but this provides a working solution

<?php namespace App\Extensions\Foundation;

use Symfony\Component\HttpFoundation\RedirectResponse as SymfonyRedirect;

class Application extends \Illuminate\Foundation\Application {

    /**
     * Redirect the request if it has a trailing slash.
     *
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|null
     */
    public function redirectIfTrailingSlash($excludePaths = array())
    {
        if ($this->runningInConsole()) return;

        // Here we will check if the request path ends in a single trailing slash and
        // redirect it using a 301 response code if it does which avoids duplicate
        // content in this application while still providing a solid experience.
        $path = $this['request']->getPathInfo();

        if ($path != '/' and ends_with($path, '/') and ! ends_with($path, '//') and ! in_array($path, $excludePaths))
        {
            with(new SymfonyRedirect($this['request']->fullUrl(), 301))->send();

            exit;
        }
    }
}

And the last thing to do is change the creation of Application call

// bootstrap/start.php
//$app = new Illuminate\Foundation\Application;
$app = new App\Extensions\Foundation\Application;
$app->redirectIfTrailingSlash(array('tab/'));

Note you’ll need to load the extended Application class 🙂

EDIT:
There are changes with Facebook and L4.1

A facebook page tab doesn’t require the trailing slash at the end of the URL anymore but the canvas page still does.

L4.1 redirects the trailing slash at .htaccess not in the foundation application class as it did previously

# Redirect Trailing Slashes…
RewriteRule ^(.*)/$ /$1 [L,R=301]

Just comment it out

Written by admin

August 21st, 2013 at 1:58 pm

Posted in Facebook,Laravel

Powered by CDN Rewrites