Symfony2:从控制器回显 JSON 以在 ExtJS 4 网格中使用 [英] Symfony2: Echoing JSON From a Controller for Use in an ExtJS 4 Grid

查看:12
本文介绍了Symfony2:从控制器回显 JSON 以在 ExtJS 4 网格中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 Symfony2,我正在尝试找出从控制器(例如,People)回显 JSON 以在 ExtJS 4 网格中使用的正确方法.

I'm just getting started with Symfony2 and I'm trying to figure out what the correct approach is for echoing out JSON from a controller (e.g., People) for use in an ExtJS 4 grid.

当我使用 vanilla MVC 方法做所有事情时,我的控制器会调用类似 getList 的方法,它会调用 People 模型的 getList> 方法,获取这些结果并执行以下操作:

When I was doing everything using a vanilla MVC approach, my controller would have method called something like getList that would call the People model's getList method, take those results and do something like this:

<?php
class PeopleController extends controller {
    public function getList() {
        $model = new People();
        $data = $model->getList();
        echo json_encode(array(
            'success' => true,
            'root' => 'people',
            'rows' => $data['rows'],
            'count' => $data['count']
        ));
    }
}
?>

  • Symfony2 中的这种行为是什么样的?
  • 控制器是否适合这种行为?
  • 解决此类问题的最佳实践是什么(在 Symfony 中)?
  • 推荐答案

    控制器是否适合这种行为?

    Is the controller the right place for this kind of behavior?

    是的.

    Symfony2 中的这种行为是什么样的?

    What does this kind of behavior look like in Symfony2?

    解决此类问题的最佳实践(在 Symfony 中)是什么?

    What are the best practices (within Symfony) for solving this kind of problem?

    在 symfony 中它看起来非常相似,但有一些细微差别.

    In symfony it looks pretty much alike, but there are couple of nuances.

    我想推荐我对这些东西的方法.让我们从路由开始:

    I want to suggest my approach for this stuff. Let's start from routing:

    # src/Scope/YourBundle/Resources/config/routing.yml
    
    ScopeYourBundle_people_list:
        pattern:  /people
        defaults: { _controller: ScopeYourBundle:People:list, _format: json }
    

    _format 参数不是必需的,但稍后您会看到为什么它很重要.

    The _format parameter is not required but you will see later why it's important.

    现在我们来看看控制器

    <?php
    // src/Scope/YourBundle/Controller/PeopleController.php
    namespace OverseerMainBundleController;
    
    use SymfonyBundleFrameworkBundleControllerController; 
    
    class PeopleController extends Controller
    {   
      public function listAction()
      {
        $request = $this->getRequest();
    
        // if ajax only is going to be used uncomment next lines
        //if (!$request->isXmlHttpRequest())
          //throw $this->createNotFoundException('The page is not found');
    
        $repository = $this->getDoctrine()
              ->getRepository('ScopeYourBundle:People');
    
        // now you have to retrieve data from people repository.
        // If the following code looks unfamiliar read http://symfony.com/doc/current/book/doctrine.html
        $items = $repository->findAll();
        // or you can use something more sophisticated:
        $items = $repository->findPage($request->query->get('page'), $request->query->get('limit'));
        // the line above would work provided you have created "findPage" function in your repository
    
        // yes, here we are retrieving "_format" from routing. In our case it's json
        $format = $request->getRequestFormat();
    
        return $this->render('::base.'.$format.'.twig', array('data' => array(
          'success' => true,
          'rows' => $items,
          // and so on
        )));
      }
      // ...
    }    
    

    控制器以路由配置中设置的格式呈现数据.在我们的例子中,它是 json 格式.

    Controller renders data in the format which is set in the routing config. In our case it's the json format.

    以下是可能的模板示例:

    Here is example of possible template:

    {# app/Resourses/views/base.json.twig #}
    {{ data | json_encode | raw }}
    

    这种方法的优点(我的意思是使用 _format)是,如果你决定从 json 切换到,例如,xml 没有问题 - 只需在路由配置中替换 _format,当然,创建相应的模板.

    The advantage of this approach (I mean using _format) is that it if you decide to switch from json to, for example, xml than no problem - just replace _format in routing config and, of course, create corresponding template.

    这篇关于Symfony2:从控制器回显 JSON 以在 ExtJS 4 网格中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆