I wrote a middleware for authentication in slim framework v2.
Here is the tutorial : http://docs.slimframework.com/routing/middleware/#what-arguments-are-passed-into-each-route-middleware-callable
$app->get('/cases/shared/group/:group_id', 'authenticate', $authenticate2($app->router()->getCurrentRoute(), 'member', 'group'), function ($group_id) use ($app) {
$limit = $app->request()->params('limit');
$offset = $app->request()->params('offset');
$search = $app->request()->params('search');
$order = $app->request()->params('order');
$objCases = new Cases();
$result = $objCases->getSharedWithGroupCases($group_id, $limit, $offset, $search, $order);
if ($result === null) {
$response["error"] = true;
$response["message"] = _("İlgili grup ile paylaşılan olgu bulunmamaktadır.");
} else {
$response["error"] = false;
$response["data"] = $result;
$response["total_data"] = $objCases->getNumOfSharedWithGroupCasesData($group_id, $search, $order);
}
echoResponse(200, $response);
});
Authentications. :
function authenticate(\Slim\Route $route) {
$headers = getallheaders();
$response = array();
$app = \Slim\Slim::getInstance();
if (isset($headers['Authorization'])) {
if(strpos($headers['Authorization'], '.') !== false) {
$authorization = explode('.', $headers['Authorization']);
$api_key = $authorization[0];
$user_id = $authorization[1];
$objUsers = new Users();
if ($objUsers->isValidUser($user_id, $api_key) <= 0) {
$response["error"] = true;
$response["message"] = "Access Denied. Invalid Api key";
echoResponse(401, $response);
$app->stop();
}
} else {
$response["error"] = true;
$response["message"] = "Access Denied. Invalid Api key";
echoResponse(401, $response);
$app->stop();
}
} else {
$response["error"] = true;
$response["message"] = "Api key is misssing";
echoResponse(400, $response);
$app->stop();
}
}
$authenticate2 = function (\Slim\Route $route, $role, $where) {
$response["group_id"] = $route->getParam('group_id');
$response["role"] = $role;
$response["where"] = $where;
echoResponse(200, $response);
};
First authentcation method works well. But second authentication method returns two errors :
-
Undefined variable: authenticate2
-
Function name must be a string
Errors lines in : $app->get('/cases/shared/group/:group_id', 'authenticate', $authenticate2($app->router()->getCurrentRoute(), 'member', 'group'), function ($group_id) use ($app) {
How can i send a route and parameters to middleware together ?