简体   繁体   English

超薄框架4; 返回Formr-form

[英]Slim framework 4; return Formr-form

In Slim framework 4;在 Slim 框架 4 中; How can I return a Formr-form in my controller as a response to a get-request?如何在我的 controller 中返回 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;
    });
});

The code outputs nothing.该代码不输出任何内容。 However, if I uncomment the two lines, it outputs (as expected):但是,如果我取消注释这两行,它会输出(如预期的那样):

<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>

From Formr documentations :来自Formr 文档

Formr will automatically echo form elements and messages to the screen, and this is usually fine. Formr 会自动将表单元素和消息回显到屏幕上,这通常很好。 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.在这些情况下,只需传递单词hush,然后手动回显您的元素和消息。

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

The default value for the first parameter of Formr\Formr constructor is an empty string, so in your case you should create new Formr instance with an empty string '' as the first parameter ann 'hush' as the second parameter: Formr\Formr构造函数的第一个参数的默认值为空字符串,因此在您的情况下,您应该使用空字符串''作为第一个参数创建新的 Formr 实例,并将'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;
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM