我已经尝试了很长一段时间了,我不太明白为什么每件事都和苗条一起工作。没有Slim,一切都很好,我有点了解一切是如何工作的(这是一个小项目,我正在学习ajax、面向对象的php和Slim,从来没有这样做过,所以我真的迷路了)。
我现在拥有的是一个带有表单的html,它通过ajax将数据发送到.php文件。该文件接受数据,运行查询,将结果放入.json字符串,然后html将结果打印为charts.js画布。
这是我使用php (Select.php)的类:
<?php require_once 'Connection.php';
$id = $_POST['id'];
$from = $_POST['from'];
$to = $_POST['to'];
$date = new Select($dbh, $id, $from, $to);
return $dates->select();
class Select {
private $dbh;
public function __construct($dbh, $id, $from, $to) {
$this->dbh = $dbh;
$this->id = $id;
$this->from = $from;
$this->to = $to;
}
public function select() {
$id = $this->id;
$from = $this->from;
$to = $this->to;
$query = ***Ignoring it because it's quite long***
$results = [];
while ($arr = $query->fetch(PDO::FETCH_ASSOC)) {
$results[] = $arr;
}
echo json_encode($results);
}
}我的ajax脚本(generateChart.js):
$.ajax({
type: 'post',
url: 'classes/Select.php',
data: $('form').serialize(),
success: function (data) {
var results = JSON.parse(data);
var chartjsTemp = [];
var chartjsDate = [];
for (var i = 0; i < results.length; i++) {
chartjsTemp.push(results[i].probeTemp);
chartjsDate.push(results[i].dateProbe);
}
var ctx = document.getElementById('myChart').getContext('2d');
var button = $("#submitButton");
submitButton.addEventListener("click", function(){
myChart.destroy();
});
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: chartjsDate,
datasets: [{
label: 'temp',
data: chartjsTemp,
backgroundColor: "rgba(240,240,240,0.5)"
}]
}
});
}
});这就是我试图实现'Select.php‘(称为slimSelect.php)的地方。我知道这个文件是一个完全的火车残骸,但我几乎是通过尝试和错误,但我绝对没有前进的一点。我不理解文献资料,在堆栈溢出中的其他员额也没有完全解释它是如何工作的:
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
require 'Autoloader.php';
$app = new \Slim\App;
$app->post('/', function(Request $request) use ($app) {
$allPostPutVars = $request->getParsedBody();
$id = $allPostPutVars['id'];
$from = $allPostPutVars['from'];
$to = $allPostPutVars['to'];
include 'Select.php';
});
$app->run();现在,它给了我Fatal error: Class 'Select' not found,尽管它确实存在。如何使苗条和我的班级一起工作,这让我心神不宁。如果有人能解释我如何使它工作,或至少指出我的正确方向,我会非常感激。
编辑:修改的slimSelect.php。我现在得到“致命错误:调用未定义的方法Slim\Http\Response::getAttribute()在第11行”Edit2: get!我的主要问题之一是,我完全搞砸了$app->post中的函数,这给我带来了$request等方面的错误。魔法发生在第9行和第10行。它现在将.json发回并打印图表!现在,我想了解如何在不包括类选择的情况下调用类选择。
发布于 2017-04-24 09:46:29
选择类不是自动加载的,这就是为什么要获得此错误的原因。你看过苗条网站上的推荐信吗?https://www.slimframework.com/“开始使用Slim的最简单方法是通过运行这个bash命令来创建一个使用Skeleton作为基础的项目:
$ php composer.phar创建-项目瘦/瘦骨架我的应用程序名“。我通常使用这个框架应用程序来构建我的瘦应用程序。
https://stackoverflow.com/questions/43584332
复制相似问题