1
我想要苗條2項目轉換苗條3。 我正在stuking一個問題。苗條2到苗條3方法錯誤
當我打電話給我的方法: http://localhost:8000/task_manager/v1/tasks
我得到了一個錯誤:
Catchable fatal error: Argument 1 passed to authenticate() must
be an instance of Slim\Route, instance of
Slim\Http\Request given in
C:\wamp64\www\task_manager\v1\index.php on line 42.
我怎麼能解決這個問題?一些幫助將不勝感激。
這裏是我的PHP代碼:
$app = new \Slim\App();
...
...
function authenticate(\Slim\Route $route) {
// Getting request headers
$headers = apache_request_headers();
$data = array();
$app = AppContainer::getInstance();
// Verifying Authorization Header
if (isset($headers['Authorization'])) {
$db = new DbHandler();
// get the api key
$api_key = $headers['Authorization'];
// validating api key
if (!$db->isValidApiKey($api_key)) {
// api key is not present in users table
$data["error"] = true;
$data["message"] = "Access Denied. Invalid Api key";
return echoRespnse(401, $data, $response);
//$app->stop();
return $response->withStatus(401)->withBody("Bad Request");
} else {
global $user_id;
// get user primary key id
$user_id = $db->getUserId($api_key);
}
} else {
// api key is missing in header
$data["error"] = true;
$data["message"] = "Api key is misssing";
return echoRespnse(400, $data, $response);
}
}
$app->get('/tasks', function (Request $request, Response $response) {
global $user_id;
$data = array();
$db = new DbHandler();
// fetching all user tasks
$result = $db->getAllUserTasks($user_id);
$data["error"] = false;
$data["tasks"] = array();
// looping through result and preparing tasks array
while ($task = $result->fetch_assoc()) {
$tmp = array();
$tmp["id"] = $task["id"];
$tmp["task"] = $task["task"];
$tmp["status"] = $task["status"];
$tmp["createdAt"] = $task["created_at"];
array_push($data["tasks"], $tmp);
}
return echoRespnse(200, $data, $response);
})->add('authenticate');
既然你不是在你的'身份驗證的任何地方使用'$ route'()'函數,只是改變了'身份驗證(\ Slim \ Route $ route)'''authenticate(Request $ request,Response $ response)''。 –
如果你這樣做,你可以刪除'apache_request_headers()'並使用Slim的Request類來獲取它們:https://www.slimframework.com/docs/objects/request.html –
謝謝。我想你確定。這是解決我的問題的一個選擇。 –