2016-11-30 2 views
1

슬림 한 2 개 프로젝트를 슬림형 3으로 변환하려고합니다. 문제가 있습니다.Slim 2에서 Slim 3으로 방법 오류가 발생했습니다.

내 메서드를 호출 :

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. 

내가 그것을 어떻게 고칠 수 : http://localhost:8000/task_manager/v1/tasks

내가 다음 오류가? 어떤 도움을 주시면 감사하겠습니다.

이 Slim2에서
$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'); 
+0

당신은() 어디서나'인증 할에'$의 route'를 사용하지 않는 때문에이'기능, 단지'인증합니다 변경 (\ Slim \ Route $ route)'를'authenticate (Request $ request, Response $ response)'로 설정합니다. –

+0

위의 작업을 수행 한 경우,'apache_request_headers()'를 제거하고 Slim의 Request 클래스를 사용하여 대신 가져올 수 있습니다 : https://www.slimframework.com/docs/objects/request.html –

+0

감사합니다. 나는 네가 확신한다고 생각한다. 내 문제를 해결할 수있는 옵션입니다. –

답변

1

이 같은 미들웨어를 추가 할 수 있습니다 :

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

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

이 slim3에 수 없습니다 : 여기

내 PHP 코드 먼저 미들웨어는 add 방법으로 추가해야합니다 이렇게 :

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

두 번째 미들웨어에는가 없습니다.- 첫 번째 매개 변수로 사용할 객체입니다. 이들은 Request, Response이 아니며 다음 미들웨어 호출 가능 파일입니다. 그래서이 다음과 같이해야합니다 :

/** 
* 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)