1

我用 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;

MainKernel 类返回响应:

/**
 * 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;
}

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

第 30 行“index.html.twig”中的未知“form_widget”函数

我错过了什么?

谢谢

4

2 回答 2

1

我错过了什么?

我相信您错过了一个事实,即您没有使用 Silex,而是使用自举的 Symfony 组件,这些组件看起来与 Potencier 关于如何构建框架的博客文章非常相似(如果不一样的话) 。

您可以做的是通过删除您在此处粘贴的大部分内容来重构您的代码,因为这已经是 Silex 的一部分,然后正如@marc 建议的那样注册表单服务提供者,以及 Twig 一个(你说你有已经通过作曲家添加)。基本上,您在 Silex 中的代码可以归结为:

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

$app = new Silex\Application();

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

$app->run();

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

于 2016-06-17T22:48:35.093 回答
0

form_widget功能由TwigServiceProvider在引导期间注册的FormExtension 提供。也许您只是忘记在您的应用程序中注册 FormServiceProvider :

use Silex\Provider\FormServiceProvider;

$app->register(new FormServiceProvider());
于 2016-06-11T19:43:56.763 回答