我想在控制器外部访问我的配置变量。
当我尝试:
class pdfFooter extends \TCPDF
{
public function footer()
{
$config = $this->get('core_parameters');
}
}
我得到这个错误:
Uncaught PHP Exception Symfony\Component\Debug\Exception\UndefinedMethodException: "Attempted to call an undefined method named "get" of class "Plugin\PrintBundle\Controller\pdfFooter".
简单地调用:
$this->writeHTMLCell($config->getParameter('heading_color_config'));
触发它。我遇到的这个问题的大多数其他主题都建议将其全球化。当然有更好的方法?
刚注入容器,https://stackoverflow.com/questions/40692433/how-to-get-the-root-path-in-helper-class-symfony2/40693266#40693266 – Federkun
不管你做什么,都不要注入容器。相反,学习一些关于服务的知识,然后注入配置对象。 http://symfony.com/doc/current/service_container.html – Cerad
为什么要注入容器是坏的? (愚蠢的问题,我敢肯定,我是Symfony的新手) – billblast