这是控制器中的代码摘录
InputStream imported2Schema = ...getResourceAsStream("/com/path/to/Imported2.xsd");
Source imported2Source = new StreamSource(imported2Schema);
InputStream imported1Schema = ...getResourceAsStream("/com/path/to/Imported1.xsd");
Source imported1Source = new StreamSource(imported1Schema);
InputStream metadataSchema = ...getResourceAsStream("/com/path/to/metadata.xsd");
Source metadataSource = new StreamSource(metadataSchema);
Source[] schemaSources = new Source[] {imported2Source, imported1Source, metadataSource};
Schema schema = sf.newSchema(schemaSources);
这是我的服务功能
$service = $this->get('questions_service');
$form_question = $service->createQuestionForm($question, $this->generateUrl('create_question', array('adId' => $ad->getId())));
答案 0 :(得分:3)
createForm()
函数是Symfony's Controller class中的别名。您无法从服务中访问它。您需要将Symfony容器注入服务或注入form.factory
服务。例如:
services:
questions_service:
class: AppBundle\Service\QuestionsService
arguments: [form.factory]
然后在你的课堂上:
use Symfony\Component\Form\FormFactory;
class QuestionsService
{
private $formFactory;
public function __construct(FormFactory $formFactory)
{
$this->formFactory = $formFactory;
}
public function createQuestionForm($entity, $route)
{
$form = $this->formFactory->createForm(new QuestionType(), $entity, array(
'action' => $route,
'method' => 'POST',
));
$form
->add('submit', 'submit', array(
'label' => '>',
'attr' => array('class' => 'button button-question button-message')
));
return $form;
}