Symfony3从Controller渲染CSS文件

时间:2017-06-04 08:33:51

标签: php css symfony-3.2

我想在Symfony 3中这样做:

    public function styleAction()
{
    // Fetch it from service or whatever strategy you have
    $backgroundColor = '#ff0000';

    return $this->render(
        'AcmeMyBundle::somefile.css.twig',
        ['backgroundColor' => $backgroundColor],
        ['Content-Type' => 'text/css']
    );
}

但幸运的是我仍然遇到渲染方法的第三个参数的问题。

这是我的错误消息:

Catchable Fatal Error: Argument 3 passed to Symfony\Bundle\FrameworkBundle\Controller\Controller::render() must be an instance of Symfony\Component\HttpFoundation\Response, array given

编辑:

这是如何解决它:

public function styleAction()
{
  $response = new Response();
  $response->headers->set('Content-Type', 'text/css');
  // replace this example code with whatever you need
  return $this->render('main.css.twig', [
      'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
  ],
  $response
  );
}

0 个答案:

没有答案
相关问题