gpt4 book ai didi

symfony - Silex + Twig : form_widget() not found, 即使安装了 Symfony Bridge

转载 作者:行者123 更新时间:2023-12-04 23:06:26 31 4
gpt4 key购买 nike

我一直在关注以下资源中的文档

http://silex.sensiolabs.org/doc/providers/form.html

但我收到以下错误

Twig_Error_Syntax: The function "form_widget" does not exist in "layout.html.twig"

我已经多次检查文档和我的来源,我仍然看不出哪里出错了,我错过了什么?我安装了 Symfony Twig Bridge。
<?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.*"
}
}

最佳答案

您需要先注册表单提供程序,然后注册 Twig 提供程序。

Twig 提供者 checks if the form provider is registered ,并且仅在必要时启用与表单相关的 Twig 扩展。

关于symfony - Silex + Twig : form_widget() not found, 即使安装了 Symfony Bridge,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11161092/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com