未知 "form_widget" 函数

Unknown "form_widget" function

我使用 Silex 创建了一个应用程序。

这里是 web/index.php

use Symfony\Component\Debug\Debug;
use Symfony\Component\HttpFoundation\Request;
use Application\MainKernel;

require_once __DIR__.'/../vendor/autoload.php';

Debug::enable(E_ALL & ~E_NOTICE);

$request = Request::createFromGlobals();
$kernel = new MainKernel(require_once __DIR__.'/../bootstrap.php');

$response = $kernel->handle($request);
$response->prepare($request);
$response->send();

bootstrap.php

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;

$configDir = __DIR__ . '/config';
$cacheDir = __DIR__ . '/cache';

$class = 'ApplicationContainer';
$path = sprintf($cacheDir.'%s.php', $class);

if (!file_exists($path)) {
    $container = new ContainerBuilder();
    $container->setParameter('kernel.cache_dir', realpath($cacheDir));
    $container->setParameter('kernel.config_dir', realpath($configDir));
    $container->setParameter('kernel.views_dir', realpath(__DIR__.'views'));

    $loader = new YamlFileLoader($container, new FileLocator(realpath($configDir)));
    $loader->load('services.yml');

    /* Recherche de l'user */
    $user = "";
    if (preg_match('@^/~([^/]+)/.*$@', $_SERVER['REQUEST_URI'], $m)) {
        $user = $m[1];
    }

    $container->setParameter('domain', 'digitick.local');
    $container->setParameter('user', $user);
    $container->compile();

    $dumper = new PhpDumper($container);
    file_put_contents($path, $dumper->dump(['class' => $class]));
}

require_once $path;

$container = new $class();

return $container;

Class MainKernel returns 响应:

/**
 * Creation Response Object
 * @param   Request     $request
 * @param   int         $type
 * @param   bool        $catch
 * @return  Response
 */
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
{
    if($this->container->has('request_context')) {
        $context = $this->container->get('request_context');
        $context->fromRequest($request);
    }

    try {
        $request->attributes->add($this->container->get('router')->match($request->getPathInfo()));
        $response = $this->executeController($request);
    } catch (ResourceNotFoundException $e) {
        $response = new Response('Page Not Found', Response::HTTP_NOT_FOUND);
    } catch (MethodNotAllowedException $e) {
        $response = new Response('Method Not Allowed', Response::HTTP_METHOD_NOT_ALLOWED);
    } catch (HttpException $e) {
        $code = $e->getStatusCode();
        $response = new Response(Response::$statusTexts[$code], $code);
    } catch (\Exception $e) {
        var_dump($e->getMessage());
        $response = new Response('Internal Server Error', Response::HTTP_INTERNAL_SERVER_ERROR);
    }

    return $response;
}

private function executeController(Request $request)
{
    if(!$request->attributes->has('_controller')) {
        throw new \RuntimeException('No controller found to execute');
    }

    $class = $request->get('_controller');
    if (!class_exists($class)) {
        throw new \RuntimeException(sprintf('Controller class "%s" does not exist or is not autoladable.', $class));
    }

    if(!method_exists($class, '__invoke')) {
        throw new \RuntimeException(sprintf('Controller class "%s" must have __invoke() method.', $class));
    }

    $controller = new $class();
    if($controller instanceof AbstractAction) {
        $controller->setContainer($this->container);
    }

    $response = call_user_func_array($controller, [$request]);
    if(!$response instanceof Response) {
        throw new \RuntimeException(sprintf('Controller class "%s" must return a Response object.', $class));
    }

    return $response;
}

我在 composer 上有 twig 和 twig 扩展。不幸的是,当我将 form_widget() 放入我的树枝文件中时,它说:

Unknown "form_widget" function in "index.html.twig" at line 30

我错过了什么?

谢谢

form_widget功能由FormExtension which is registered during the boot by the TwigServiceProvider提供。也许您只是忘记在您的应用程序中注册 FormServiceProvider :

use Silex\Provider\FormServiceProvider;

$app->register(new FormServiceProvider());

What am I missing ?

我相信您忽略了这样一个事实,即您没有使用 Silex,而是引导了 Symfony 组件,它们看起来与 Potencier 在 how to build your framework 上的博客文章非常相似(如果不相同的话)。

你可以做的是通过删除大部分你粘贴在这里的东西来重构你的代码,因为它已经是 Silex 的一部分,然后按照@marc 的建议注册表单服务提供者,以及 Twig 之一(你说你已经通过作曲家添加了)。基本上,您在 Silex 中的代码可以归结为:

require_once __DIR__.'/../vendor/autoload.php';

$app = new Silex\Application();

$app->get('/', function () use ($app) {
    return 'works';
});

$app->run();

请仔细阅读 Silex docs 以便更好地掌握它。