Slim 2 to Slim 3方法错误

时间:2016-11-30 16:11:37

标签: php slim

我正在尝试将苗条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');

1 个答案:

答案 0 :(得分:1)

在Slim2中你可以像这样添加中间件:

<?php
$aBitOfInfo = function (\Slim\Route $route) {
    echo "Current route is " . $route->getName();
};

$app->get('/foo', $aBitOfInfo, function () {
    echo "foo";
});

这在slim3中是不可能的:首先必须使用add方法添加中间件,如下所示:

$app->get('/foo', function () {
    echo "foo";
})->add($aBitOfInfo);

其次,中间件没有\Slim\Route - Object作为第一个参数。 这些不是RequestResponse和下一个中间件的可调用者。所以这应该是这样的:

/**
 * Example middleware closure
 *
 * @param  \Psr\Http\Message\ServerRequestInterface $request  PSR7 request
 * @param  \Psr\Http\Message\ResponseInterface      $response PSR7 response
 * @param  callable                                 $next     Next middleware
 *
 * @return \Psr\Http\Message\ResponseInterface
 */
$aBitOfInfo = function ($request, $response, $next) {
    $response->getBody()->write('BEFORE');
    $response = $next($request, $response);
    $response->getBody()->write('AFTER');

    return $response;
};

(来源https://www.slimframework.com/docs/concepts/middleware.html