我有以下控制器:
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');
返回的配置文件,我可以将它们放到一个抽象类中。
这可能吗?
答案 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);
}
}