如何重构这段 symfony 代码?
在action.class.php中:
$form = new NewsForm();
$form->setWidget('thumbnail', new sfWidgetFormSelect(array('choices' => $news['images'])));
$form->getWidget('summarize')->setDefault($news['summarize']);
$form->getWidget('title')->setDefault($news['title']);
其中$news
是前面步骤生成的;
看起来很多余,如何重构呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么,除非您在多个操作中使用相同的代码,否则您的代码并不是多余的。
为了使其可重用,您可以使用 options 参数“nofollow noreferrer”>表单构造函数并按如下方式更改表单:
您可以使用以下方式创建表单:
参考:
sfForm - getOption
Well your code is not redunant until you use the same code in mutliple actions.
To make it reusable, you can make use of the
options
parameter in the the form constructor and change the form as follows:The you can create the form with:
Reference:
sfForm - getOption