【问题标题】:Render view using response object使用响应对象渲染视图
【发布时间】:2018-08-06 06:40:58
【问题描述】:

我正在 symfony 3 中开发一个项目,我有以下代码返回一个响应实例

public function dashboardAction()
{
    return parent::dashboardAction();
}

以上代码的父方法是:

public function dashboardAction()
{
    $blocks = [
        'top' => [],
        'left' => [],
        'center' => [],
        'right' => [],
        'bottom' => [],
    ];

    foreach ($this->container->getParameter('sonata.admin.configuration.dashboard_blocks') as $block) {
        $blocks[$block['position']][] = $block;
    }

    $parameters = [
        'base_template' => $this->getBaseTemplate(),
        'admin_pool' => $this->container->get('sonata.admin.pool'),
        'blocks' => $blocks,
    ];

    if (!$this->getCurrentRequest()->isXmlHttpRequest()) {
        $parameters['breadcrumbs_builder'] = $this->get('sonata.admin.breadcrumbs_builder');
    }

    return $this->render($this->getAdminPool()->getTemplate('dashboard'), $parameters);
}

我想将变量articles 传递给响应实例中的视图。

我试着做这样的事情

return $this->render(parent::dashboardAction(), array(
        'articles' => $articles,
    ));

但它不起作用。有什么帮助吗?

【问题讨论】:

    标签: php symfony sonata-admin sonata


    【解决方案1】:

    $this->render 返回一个 Response 对象,第一个参数是要渲染的模板的名称(一个字符串)。您试图再次将 Response 对象作为第一个参数传递给 render,这显然行不通。

    同样$this->render 会使用提供的参数逐字呈现模板。这意味着您的父dashboardAction 的返回值返回一个响应对象,其中包含$this->getAdminPool()->getTemplate('dashboard') 返回的模板的呈现HTML。如果您不想实际使用完全渲染的 HTML(不,您不想这样做),那么此时您无法更改渲染的 HTML。

    总的来说,我认为直接重用操作方法不是一个好主意。相反,您可以执行以下操作:

    在你的父类中创建一个方法getDashboardParameters,它只返回render函数的参数,不调用render

    protected function getDashboardParameters()
    {
        $blocks = [
            'top' => [],
            'left' => [],
            'center' => [],
            'right' => [],
            'bottom' => [],
        ];
    
        foreach ($this->container->getParameter('sonata.admin.configuration.dashboard_blocks') as $block) {
            $blocks[$block['position']][] = $block;
        }
    
        $parameters = [
            'base_template' => $this->getBaseTemplate(),
            'admin_pool' => $this->container->get('sonata.admin.pool'),
            'blocks' => $blocks,
        ];
    
        if (!$this->getCurrentRequest()->isXmlHttpRequest()) {
            $parameters['breadcrumbs_builder'] = $this->get('sonata.admin.breadcrumbs_builder');
        }
    
        return $parameters;
    }
    

    将您的父仪表板操作更改为如下所示:

    public function dashboardAction() {
        return $this->render($this->getAdminPool()->getTemplate('dashboard'), $this->getDashboardParameters());
    }
    

    将您的子仪表板操作更改为如下所示:

    public function dashboardAction() {
        $parameters = $this->getDashboardParameters();
        $parameters['articles'] = $articles;
        return $this->render($this->getAdminPool()->getTemplate('dashboard'), $parameters);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-14
      • 2015-10-05
      相关资源
      最近更新 更多