我用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;
}
我在作曲家身上得到了树枝和树枝的延伸。不幸的是,当我在我的twig文件中放入一个form_widget()时,它说:
第30行“index.html.twig”中的未知“form_widget”函数
我错过了什么?
由于
答案 0 :(得分:1)
我错过了什么?
我相信你错过了你不使用Silex的事实,而是错误地引导了Symfony组件,这些组件看起来与Potencier在{{上的博客文章中看起来非常相似(如果不相同) 3}}
您可以做的是通过删除您在此处粘贴的大部分内容来重构您的代码,因为它已经是Silex的一部分,然后当@marc建议注册表单服务提供程序,以及Twig之一(您说你已经通过作曲家添加了)。基本上,你在Silex中的代码可以归结为:
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
$app->get('/', function () use ($app) {
return 'works';
});
$app->run();
请仔细阅读Silex how to build your framework,以便更好地掌握它。
答案 1 :(得分:0)
use Silex\Provider\FormServiceProvider;
$app->register(new FormServiceProvider());
功能由FormExtension提供,TwigServiceProvider在{{3}}启动时注册。也许你只是忘了在你的应用程序中注册FormServiceProvider:
{{1}}