最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Using Symfony with Backbone.js - Stack Overflow

matteradmin8PV0评论

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

Post a comment

comment list (0)

  1. No comments so far