最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Using Symfony with Backbone.js - Stack Overflow

programmeradmin1浏览0评论

I wanted to get views from the symfony munity about how one would achieve using backbone.js to create a one paged application.
Is it even possible to call the functions in symfony (e.g public function executeCreate()) through backbone.js) has anyone done it?
Can you give me some resources? thanks in advance.

I wanted to get views from the symfony munity about how one would achieve using backbone.js to create a one paged application.
Is it even possible to call the functions in symfony (e.g public function executeCreate()) through backbone.js) has anyone done it?
Can you give me some resources? thanks in advance.

Share Improve this question edited Sep 20, 2012 at 19:23 j0k 22.8k28 gold badges81 silver badges90 bronze badges asked Apr 27, 2012 at 16:16 underscore666underscore666 1,7395 gold badges24 silver badges38 bronze badges 1
  • Of course you cannot directly access a php method through javascript (client vs. server). You would have to provide a API of some kind (REST anyone?) which the js could use to access and post data. – Sgoettschkes Commented Apr 27, 2012 at 21:41
Add a ment  | 

3 Answers 3

Reset to default 2

Why not? Symfony easily can return JSON data on request by backbone clientside app. For Symfony2 it can be like this:

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class MyController extends Controller {
    public function createAction($requestParam1, $requestParam2)
        $response = new Response(json_encode(array(
            'requestParam1' => $requestParam1,
            'requestParam2' => $requestParam2
        )));
        $response->headers->set('Content-Type', 'application/json');

        return $response;
    }
}

I would remend you to have a look at the FOSRestBundle. Another attempt could be to handle the requests yourself, and return the date from the orm/odm via Serializer

the only thing bothered me so far, was the form handling (used backbone-forms), where I simply not was able to keep my code DRY (had duplicate data for the form+validation in symfony and in the frontend-part).

If you want to start a new Symfony2 + backbone app, I would remend this bundle:

https://github./gigo6000/DevtimeBackboneBundle

There's also a demo app that can be helpful to see the backbone.js and Symfony2 interaction:

https://github./gigo6000/DevtimeRafflerBundle

发布评论

评论列表(0)

  1. 暂无评论