Search code examples
phptwigsilextwig-extension

Unknown "form_widget" function


I created an application with Silex.

Here is the 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();

The 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 responses :

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

I've got the twig and twig extensions on composer. Unfortunelly, when I put a form_widget() inside my twig file, it says :

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

What am I missing ?

Thanks


Solution

  • What am I missing ?

    I believe you're missing the fact that you're not using Silex, but rather bootstrapped Symfony components, which look pretty similar (if not the same) as Potencier's blog posts on how to build your framework.

    What you can do is refactor your code by removing most of the stuff you have pasted here because that is already part of Silex, and then as @marc suggested register the form service provider, as well as the Twig one (which you said you have already added via composer). Basically, your code in Silex would boil down to this:

    require_once __DIR__.'/../vendor/autoload.php';
    
    $app = new Silex\Application();
    
    $app->get('/', function () use ($app) {
        return 'works';
    });
    
    $app->run();
    

    Please read through the Silex docs carefully in order to grasp it better.