以下のリソースのドキュメントに従っています
http://silex.sensiolabs.org/doc/providers/form.html
しかし、次のエラーが発生します
Twig_Error_Syntax: The function "form_widget" does not exist in "layout.html.twig"
ドキュメントとソースを何度もチェックしましたが、どこが間違っているのかわかりません。何が欠けていますか?SymfonyTwigBridgeをインストールしています。
<?php
require_once __DIR__.'/vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Yaml\Parser;
use Silex\Provider\FormServiceProvider;
$app = new Silex\Application();
$app['debug'] = true;
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__. '/src/views',
'twig.options' => array(
'debug' => true,
'cache' => false
),
));
$app['twig']->addExtension(new Twig_Extension_Debug());
$app['yaml'] = new Parser();
//.....
$app->register(new FormServiceProvider());
$app->register(new Silex\Provider\ValidatorServiceProvider());
$site['name'] = 'My Site';
// More definitions for $site
$app->match('/', function() use($app, $site) {
return $app['twig']->render('layout.html.twig', $site);
})->method('POST|GET');
$app->match('/form/', function(Request $request) use($app, $site) {
$data = array(
'name' => 'Your name',
'email' => 'Your email',
);
$form = $app['form.factory']->createBuilder('form', $data)
->add('name')
->getForm();
if ('POST' == $request->getMethod()) {
$form->bindRequest($request);
if ($form->isValid()) {
$data = $form->getData();
// do something with the data - I haven't got that far yet
// redirect somewhere
return $app->redirect('/');
}
}
$site['form'] = $form->createView();
return $app['twig']->render('form.html.twig', $site);
})->method('POST|GET');
$app->run();
編集:composer.jsonファイル
{
"require": {
"silex/silex" : "dev-master",
"symfony/config" : "dev-master",
"symfony/yaml" : "dev-master",
"doctrine/common" : ">=2.1,<2.3-dev",
"doctrine/dbal" : ">=2.1,<2.3-dev",
"symfony/dependency-injection": "dev-master",
"symfony/console" : "dev-master",
"monolog/monolog" : ">=1.0.0",
"twig/twig" : ">=1.2.0",
"symfony/form" : "2.1.*",
"symfony/translation": "2.1.*",
"symfony/twig-bridge": "2.1.*",
"symfony/validator": "2.1.*"
}
}