开发者

ReSTful Facebook canvas Symfony2 application

开发者 https://www.devze.com 2023-03-12 15:52 出处:网络
As you may know, in order to pass user\'s informations(signed_request) to your app, Facebook access canvas(ie: iframe) applications by

As you may know, in order to pass user's informations(signed_request) to your app, Facebook access canvas(ie: iframe) applications by sending them a POST request. This 开发者_运维问答mechanism is explained here.

In order to keep ReSTful, what would be the right place in Symfony (which service, file...) to implement this Ruby on Rails' trick Pierre Olivier Martel descibes here: http://blog.coderubik.com/2011/03/restful-facebook-canvas-app-with-ra... , eg: convert every POST requests containing a 'signed_request' parameter to a GET one?


You could implement a RequestListener like it is done in the RESTBundle: https://github.com/FriendsOfSymfony/RestBundle/blob/master/EventListener/RequestListener.php


Inspired from Stuck's answer(thanks!) and from Symfony Cookbook:

# src/Acme/FacebookBundle/RequestListener.php

namespace Acme\FacebookBundle;

use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class RequestListener
{
    public function onCoreRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();
        if ('POST' == $request->getMethod() && null !== $request->get('signed_request'))
        {
            $request->setMethod('GET');
        }
    }
}

Service Definition:

# app/config/config.yml

services:
    acme.facebookbundle.listener.request:
        class: Acme\FacebookBundle\RequestListener
        tags:
            - { name: kernel.listener, event: core.request, method: onCoreRequest }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号