Php 我们使用Slim框架运行restful API时出现未捕获异常
我想为我的android应用程序创建一个使用Slim的API。API非常简单,我只想在我的数据库中保存一篇文章的数据。我的代码如下所示Php 我们使用Slim框架运行restful API时出现未捕获异常,php,android,web-services,slim,restful-architecture,Php,Android,Web Services,Slim,Restful Architecture,我想为我的android应用程序创建一个使用Slim的API。API非常简单,我只想在我的数据库中保存一篇文章的数据。我的代码如下所示 <?php error_reporting(-1); ini_set('display_errors', 'On'); require_once '../include/DbHandler.php'; require '.././libs/Slim/Slim.php'; \Slim\Slim::registerAutoloader(); $app =
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
require_once '../include/DbHandler.php';
require '.././libs/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
/**
* Verifying required params posted or not
*/
function verifyRequiredParams($required_fields) {
$error = false;
$error_fields = "";
$request_params = array();
$request_params = $_REQUEST;
if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
$app = \Slim\Slim::getInstance();
parse_str($app->request()->getBody(), $request_params);
}
foreach ($required_fields as $field) {
if (!isset($request_params[$field]) || strlen(trim($request_params[$field])) <= 0) {
$error = true;
$error_fields .= $field . ', ';
}
}
if ($error) {
$response = array();
$app = \Slim\Slim::getInstance();
$response["error"] = true;
$response["message"] = 'Required field(s) ' . substr($error_fields, 0, -2) . ' is missing or empty';
echoRespnse(400, $response);
$app->stop();
}
}
/**
* Echoing json response to client
* @param String $status_code Http response code
* @param Int $response Json response
*/
function echoRespnse($status_code, $response) {
$app = \Slim\Slim::getInstance();
$app->status($status_code);
$app->contentType('application/json');
echo json_encode($response);
}
/**
* Creating new task in db
* method POST
* params - name
* url - /articles/
*
* {"error": false,
"message": "Task created successfully",
"task_id": 1}
*/
$app->post('/articles', 'authenticate', function() use ($app) {
//verifyRequiredParams(array('article'));
$response = array();
$article_image = $app->request->post('article_image');
$article_video = $app->request->post('article_video');
$article_title = $app->request->post('article_title');
$article_main_body = $app->request->post('article_main_body');
$article_tags = $app->request->post('article_tags');
$db = new DbHandler();
// creating new article
$article_id = $db->createTask($article_image,$article_video,$article_title,$article_main_body,$article_tags);
if ($article_id != NULL) {
$response["error"] = false;
$response["message"] = "Article created successfully";
$response["article_id"] = $article_id;
} else {
$response["error"] = true;
$response["message"] = "Failed to create article. Please try again";
}
echoRespnse(201, $response);
});
/**
* Listing all articles
* method GET
* url /articles
* {
"error": false,
"tasks": [
{
"id": 1,
"task": "Complete REST article by Sunday",
"status": 0,
"createdAt": "2014-01-08 23:35:45"
},
{
"id": 2,
"task": "Book bus tickets!",
"status": 0,
"createdAt": "2014-01-08 23:56:52"
}
]
}
*/
$app->get('/articles', 'authenticate', function() {
$response = array();
$db = new DbHandler();
$result = $db->getAllTasks();
$response["error"] = false;
$response["articles"] = array();
// looping through result and preparing articles array
while ($article = $result->fetch_assoc()) {
$tmp = array();
$tmp["id"] = $task["id"];
$tmp["article_image"] = $article["article_image"];
$tmp["article_video"] = $article["article_video"];
$tmp["article_title"] = $article["article_title"];
$tmp["article_main_body"] = $article["article_main_body"];
$tmp["article_tags"] = $article["article_tags"];
$tmp["created_at"] = $article["created_at"];
array_push($response["articles"], $tmp);
}
echoRespnse(200, $response);
});
/**
* Listing single task of particual user
* method GET
* url /articles/:id
* Will return 404 if the task doesn't belongs to user
*
* {
"error": false,
"id": 2,
"task": "Book bus tickets!",
"status": 0,
"createdAt": "2014-01-08 23:56:52"
}
*/
$app->get('/articles/:id', 'authenticate', function($article_id) {
$response = array();
$db = new DbHandler();
// fetch article
$result = $db->getArticle($article_id);
if ($result != NULL) {
$response["error"] = false;
$response["id"] = $result["id"];
$response["article_image"] = $result["article_image"];
$response["article_video"] = $result["article_video"];
$response["article_title"] = $result["article_title"];
$response["article_main_body"] = $result["article_main_body"];
$response["article_tags"] = $result["article_tags"];
$response["created_at"] = $result["created_at"];
echoRespnse(200, $response);
} else {
$response["error"] = true;
$response["message"] = "The requested resource doesn't exists";
echoRespnse(404, $response);
}
});
$app->run();
?>
我的项目结构是
include
|
-Config.php
-DbConnect.php
-DbHandler.php
libs
|
-Slim
v1
|
-.htaccess
-index.php
我在DigitalOcean中创建的Ubuntu Droplet中上传了我的项目,但是当我运行我的项目时,我得到了下面的异常
未捕获异常“InvalidArgumentException”所有路由中间件必须可调用
我的服务器配置文档中有什么需要更改的吗?Slim正在尝试调用一个不存在的函数 在每个路由定义中,都有一个值为
'authenticate'
的第二个参数(例如在$app->post('/articles','authenticate',function()中使用($app){});
在执行以下操作之前,必须在某个位置定义authenticate
函数:
function authenticate() {
echo "My authenticate logic!";
}
请查看。Slim正在尝试调用一个不存在的函数 在每个路由定义中,都有一个值为
'authenticate'
的第二个参数(例如在$app->post('/articles','authenticate',function()中使用($app){});
在执行以下操作之前,必须在某个位置定义authenticate
函数:
function authenticate() {
echo "My authenticate logic!";
}
请看下面的图片