5

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 = 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):

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

4

4 回答 4

14

解决此问题的简单方法是使用 getInstance();

function yourFunction(){
   $app = \Slim\Slim::getInstance();
}
于 2013-09-30T08:29:40.410 回答
5

您始终可以将 $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() .'}}'; 
    }
}
于 2013-09-04T02:37:32.437 回答
2

将数据库调用和应用程序逻辑/表示分开会更干净。我会立即返回您的“章节”对象并处理 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() .'}}'; 
  }
}
于 2015-10-30T21:08:09.297 回答
0

use除了与命名空间一起使用外,该关键字仅适用于闭包结构。所以不支持您使用常规功能所做的事情。您将不得不使用global $app通常被认为是不好的做法。

于 2013-07-28T15:01:54.387 回答