1

我正在创建我的第一个 Userfrosting 应用程序并尝试一些简单的事情,但遇到了第一个障碍。我正在尝试创建一个将波段添加到数据库表的表单:

CREATE TABLE `uf_band` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `band_name` text NOT NULL,
 `band_description` longtext NOT NULL,
 `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
 `modified_at` timestamp NULL DEFAULT NULL,
 PRIMARY KEY (`id`)
) 

初始化.php:

$table_band = new \UserFrosting\DatabaseTable($app->config('db')['db_prefix'] . "band", [
    "band_name",
    "band_description",
    "created_at",
    "modified_at"
]);

路线的 index.php:

$app->get('/bands/new/?', function () use ($app) {
    $controller = new UF\BandController($app);
    return $controller->newBand();
});

使用 GET 的 New Band 表单可以正常工作。带控制器.php

public function newBand(){
    if (!$this->_app->user->checkAccess('uri_bands')){
        $this->_app->notFound();
    }
    $this->_app->render('bands/new.twig', []);
}

如果我将它添加到 newBand 控制器进行测试,它会在我加载新的乐队表单时写入数据库,这很好:

    $new_band = new Band([
        "band_name" => "band_name",
        "band_description" => "band_description"
    ]);
    $new_band->save();
    $id = $new_band->id;

问题是使用 Post 保存。即使我在值中硬编码并且甚至不尝试读取 POST 数据,我得到的只是一个空白屏幕。apache error.log 中没有错误消息,也没有任何内容:

索引.php

$app->post('/bands/new/?', function () use ($app) {    
    $controller = new UF\BandController($app);
    return $controller->saveBand();
});

带控制器.php

public function saveBand(){
    $new_band = new Band([
        "band_name" => "band_name",
        "band_description" => "band_description"
    ]);
    $new_band->save();
    $id = $new_band->id;
}

如果我将我的 POST 路线替换为

$app->post('/bands/new/?', function () use ($app) {    
    echo 'post';
})

我仍然只是得到一个空白屏幕。

这是bands/new.twig,但我认为问题出在此之前:

{% extends "layouts/layout-dashboard.twig" %}

{% set page_group = "dashboard" %}

{% block page %}     
    {% set page = page | merge({
        "title"          : "Add New Band",
        "description" : ""
    }) %}
    {{ parent() }}
{% endblock %}

{% block content %}
<h1>{{page.title}}</h1>
<p>{{page.description}}</p>

<div class="row">
    <div class="col-lg-6">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title"><i class="fa fa-users"></i>Add New Band</h3>
            </div>
            <div class="panel-body">
                <form class="form-horizontal" role="form" name="bands" action="{{site.uri.public}}/bands/new" method="post">

                    <div class="form-group">
                        <label for="input_band" class="col-sm-4 control-label">Band Name</label>
                        <div class="col-sm-8">
                            <input type="text" id="input_title" class="form-control" name="band_name" placeholder="Please enter the band name">
                            <!--<p class="help-block">Enter the band name here</p>-->
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="input_title" class="col-sm-4 control-label">Description</label>
                        <div class="col-sm-8">
                            <textarea type="text" id="input_title" class="form-control" name="band_description" placeholder="Please enter a description of the band.  This may be displayed publically"></textarea>
                            <!--<p class="help-block">This will become the new title for all users in the selected group.</p> -->
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="input_group" class="col-sm-4 control-label">Genre</label>
                        <div class="col-sm-8">
                            <select id="input_group" class="form-control select2" name="genre_id">
                                {% for group in groups %}
                                    <option value="{{group.id}}">{{group.name}}</option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>

                    <div class="form-group text-center">
                        <button type="submit" class="btn btn-success text-center">Add Band</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

{% endblock %}
{% block page_scripts %}
    <script>

$(document).ready(function() { 
    // Load the validator rules for this form
    var validators = {{validators | raw}};
    ufFormSubmit(
      $("form[name='add_band']"),
      validators,
      $("#userfrosting-alerts"),
      function(data, statusText, jqXHR) {
          // Reload the page on success
          window.location.reload(true);   
      }
    );
});

</script>
{% endblock %}  

感谢您的任何想法。没有错误消息,这让我发疯了!

4

1 回答 1

3

好像您忘记了 csrf 令牌。

您可以添加这个隐藏的输入字段:

<input type="hidden" name="{{csrf_key}}" value="{{csrf_token}}">

并在此处阅读更多内容。

于 2016-12-21T08:33:04.137 回答