I am trying to make a reusable function, but everytime I try to use $app in the remote function, I get a blank screen. Here is what does work:
我正在尝试做一个可重用的函数,但是每次我尝试在远程函数中使用$app时,我都会得到一个空白的屏幕。以下是有效的方法:
$app = new \Slim\Slim();
//GET CHAPTERS
$app->get(
'/chapters',
function () use ($app) {
$app->contentType('application/json');
executeSql('SELECT * FROM chapters ORDER BY id');
}
);
//GENERIC SQL EXECUTE
function executeSql($sql) {
try {
$db = getConnection();
$stmt = $db->query($sql);
$results = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($results);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
But I am trying to move the json header in the function and can't get this to work (gets the blank white screen):
但是我正在尝试在函数中移动json头,却无法让它工作(得到空白的白屏):
$app = new \Slim\Slim();
//GET CHAPTERS
$app->get(
'/chapters',
function () {
executeSql('SELECT * FROM chapters ORDER BY id');
}
);
//GENERIC SQL EXECUTE
function executeSql($sql) use ($app) {
$app->contentType('application/json');
try {
$db = getConnection();
$stmt = $db->query($sql);
$results = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($results);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
Is something wrong with my syntax or how I'm using PHP? I also tried without the "use ($app)" at all but still same problem.
我的语法或PHP的使用有问题吗?我也试过不用“使用($app)”,但还是一样的问题。
4 个解决方案
#1
13
Easy way to fix this is to use getInstance();
解决这个问题的简单方法是使用getInstance();
function yourFunction(){
$app = \Slim\Slim::getInstance();
}
#2
5
You could always pass the $app object as an argument to your function.
可以将$app对象作为参数传递给函数。
$app = new \Slim\Slim();
//GET CHAPTERS
$app->get(
'/chapters',
function () use ($app) {
executeSql($app, 'SELECT * FROM chapters ORDER BY id');
}
);
//GENERIC SQL EXECUTE
function executeSql(\Slim\Slim $app, $sql) {
$app->contentType('application/json');
try {
$db = getConnection();
$stmt = $db->query($sql);
$results = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($results);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
#3
2
It is cleaner to keep database calls and app logic/presentation separate. I would return your "chapters" object immediately and handle anything else outside of the executeSql function.
将数据库调用和应用程序逻辑和表示分离开来会更清晰。我将立即返回您的“章节”对象,并处理executeSql函数之外的任何其他内容。
$app = new \Slim\Slim();
// GET CHAPTERS
$app->get('/chapters', function () use ($app) {
$app->contentType('application/json');
$chapters = executeSql('SELECT * FROM chapters ORDER BY id');
if ($chapters) {
$app->response->setStatus(200);
echo json_encode($results);
} else {
$app->response->setStatus(400);
echo '{"error":{"text":"error getting chapters"}}';
}
});
// GENERIC SQL EXECUTE
function executeSql($sql) {
try {
$db = getConnection();
$stmt = $db->query($sql);
$results = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
return $results;
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
#4
0
the use
keyword only applies to closure constructs, apart from the use with namespaces. so what your doing with a regular function is not supported. you will have to use global $app
which is usually considered bad practice.
use关键字只适用于闭包构造,除了使用名称空间。所以你用常规函数做什么是不受支持的。你必须使用global $app,这通常被认为是不好的做法。
#1
13
Easy way to fix this is to use getInstance();
解决这个问题的简单方法是使用getInstance();
function yourFunction(){
$app = \Slim\Slim::getInstance();
}
#2
5
You could always pass the $app object as an argument to your function.
可以将$app对象作为参数传递给函数。
$app = new \Slim\Slim();
//GET CHAPTERS
$app->get(
'/chapters',
function () use ($app) {
executeSql($app, 'SELECT * FROM chapters ORDER BY id');
}
);
//GENERIC SQL EXECUTE
function executeSql(\Slim\Slim $app, $sql) {
$app->contentType('application/json');
try {
$db = getConnection();
$stmt = $db->query($sql);
$results = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($results);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
#3
2
It is cleaner to keep database calls and app logic/presentation separate. I would return your "chapters" object immediately and handle anything else outside of the executeSql function.
将数据库调用和应用程序逻辑和表示分离开来会更清晰。我将立即返回您的“章节”对象,并处理executeSql函数之外的任何其他内容。
$app = new \Slim\Slim();
// GET CHAPTERS
$app->get('/chapters', function () use ($app) {
$app->contentType('application/json');
$chapters = executeSql('SELECT * FROM chapters ORDER BY id');
if ($chapters) {
$app->response->setStatus(200);
echo json_encode($results);
} else {
$app->response->setStatus(400);
echo '{"error":{"text":"error getting chapters"}}';
}
});
// GENERIC SQL EXECUTE
function executeSql($sql) {
try {
$db = getConnection();
$stmt = $db->query($sql);
$results = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
return $results;
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
#4
0
the use
keyword only applies to closure constructs, apart from the use with namespaces. so what your doing with a regular function is not supported. you will have to use global $app
which is usually considered bad practice.
use关键字只适用于闭包构造,除了使用名称空间。所以你用常规函数做什么是不受支持的。你必须使用global $app,这通常被认为是不好的做法。