简单的Silex / Symfony表格

时间:2017-11-15 18:12:50

标签: php symfony silex

我对silex完全是绿色的,目前我正在尝试遵循文档并制作一个简单的表单。即使在遵循官方文档时,我也有一个我无法处理的错误。

这是我的 index.php

<?php // index.php
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();

use Silex\Provider\FormServiceProvider;
$app->register(new FormServiceProvider());

$app->register(new Silex\Provider\LocaleServiceProvider());
$app->register(new Silex\Provider\TranslationServiceProvider(), array(
    'translator.domains' => array(),
));

use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

$app->match('/', function (Request $request) use ($app) {
    // some default data for when the form is displayed the first time
    $data = array(
        'name' => 'Your name',
        'email' => 'Your email',
    );

    $form = $app['form.factory']->createBuilder(FormType::class, $data)
        ->add('name')
        ->add('email')
        ->add('billing_plan', ChoiceType::class, array(
            'choices' => array('free' => 1, 'small business' => 2, 'corporate' => 3),
            'expanded' => true,
        ))
        ->add('submit', SubmitType::class, [
            'label' => 'Save',
        ])
        ->getForm();

    $form->handleRequest($request);

    if ($form->isValid()) {
        $data = $form->getData();

        // do something with the data

        // redirect somewhere
        return $app->redirect('...');
    }

    // display the form
    return $app['twig']->render('index.twig', array('form' => $form->createView()));
});

$app['debug'] = true;
$app->run();

这是我的错误:

(1/1) RuntimeException
Controller "Closure" requires that you provide a value for the "$request" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.

这是来自https://silex.symfony.com/doc/2.0/providers/form.html的代码,我理解错误,但我不知道如何修复它,只要提供默认数据。我会提供任何帮助。

0 个答案:

没有答案