根据调用的控制器加载不同的参数文件?

时间:2014-07-02 20:44:59

标签: symfony

我有以下控制器:

class CompanyXController extends Controller
{
    public function homepageAction()
    {
        $companyinfo = array('company' => $this->container->getParameter('XConfig'));
        $this->render('hometeplate.twig', $companyinfo);
    }
}

class CompanyYController extends Controller
{
    public function homepageAction()
    {
        $companyinfo = array('company' => $this->container->getParameter('YConfig'));
        $this->render('hometeplate.twig', $companyinfo);
    }
}

由于参数字符串不同,许多代码都被复制了。

如果我可以动态更改$this->container->getParameter('Config');返回的配置文件,我可以将它们放到一个抽象类中。

这可能吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用Template method模式:

class CompanyXController extends BaseCompanyController
{
    public function getConfigParam()
    {
        return 'XConfig';
    } 
}

class CompanyYController extends BaseCompanyController
{
    public function getConfigParam()
    {
        return 'YConfig';
    } 
}

abstract class BaseCompanyController extends Controller
{
    abstract public function getConfigParam();

    public function homepageAction()
    {
        $companyinfo = array('company' => $this->container->getParameter($this->getConfigParam()));

        return $this->render('hometeplate.twig', $companyinfo);
    }
}