超薄框架 4; return Formr-表格

Slim framework 4; return Formr-form

在 Slim 框架 4 中;我如何 return 我的控制器中的 Formr 表单作为对获取请求的响应?

$app->group('/timer', function (Group $group) {
    $group->get('/', function (Request $request, Response $response) {

        $form = new Formr\Formr();

        // $form->create_form('Name')
        // die();
        $response->getBody()->write($form->create_form('Name'));
        return $response;
    });
});

代码不输出任何内容。但是,如果我取消注释这两行,它会输出(如预期的那样):

<form action="/index.php" id="myFormr" method="post" accept-charset="utf-8">
<input type="hidden" name="FormrID" value="myFormr"><label for="name">Name</label> 
<input type="text" name="name" id="name" />


<button type="submit" name="button" id="button">Submit</button>


</form>

来自Formr documentations

Formr will automatically echo form elements and messages to the screen, and this is usually fine. However, in some instances - such as when using a templating framework - this is not an option. In these cases simply pass the word hush and then manually echo your elements and messages.

$form = new Formr\Formr('bootstrap', 'hush');

Formr\Formr 构造函数的第一个参数的默认值为空字符串,因此在您的情况下,您应该使用空字符串 '' 创建新的 Formr 实例作为第一个参数 ann 'hush'作为第二个参数:


$app->group('/timer', function (Group $group) {
    $group->get('/', function (Request $request, Response $response) {
        
        // required change
        $form = new Formr\Formr('', 'hush');
             
        $response->getBody()->write($form->create_form('Name'));
        return $response;
    });
});