我用 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”函数
我错过了什么?
谢谢