Userfrosting - 开始工作,Post 空白屏幕,日志中没有错误
Userfrosting - Get Working, Post blank screen with no error in log
我正在创建我的第一个 Userfrosting 应用程序并尝试了一些简单的事情,但遇到了第一个障碍。我正在尝试创建一个将波段添加到数据库的表单 table:
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`)
)
initialize.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 表单工作正常。
BandController.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:
index.php
$app->post('/bands/new/?', function () use ($app) {
$controller = new UF\BandController($app);
return $controller->saveBand();
});
BandController.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 %}
感谢任何想法。没有错误消息,这让我抓狂!
您似乎忘记了 csrf 令牌。
您可以添加这个隐藏的输入字段:
<input type="hidden" name="{{csrf_key}}" value="{{csrf_token}}">
并阅读更多内容 here。
我正在创建我的第一个 Userfrosting 应用程序并尝试了一些简单的事情,但遇到了第一个障碍。我正在尝试创建一个将波段添加到数据库的表单 table:
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`)
)
initialize.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 表单工作正常。 BandController.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:
index.php
$app->post('/bands/new/?', function () use ($app) {
$controller = new UF\BandController($app);
return $controller->saveBand();
});
BandController.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 %}
感谢任何想法。没有错误消息,这让我抓狂!
您似乎忘记了 csrf 令牌。
您可以添加这个隐藏的输入字段:
<input type="hidden" name="{{csrf_key}}" value="{{csrf_token}}">
并阅读更多内容 here。