2016-12-19 5 views
0

파일에 몇 가지 속성을 검증하고 png로 변환하여 amazon S3 서비스로 옮겨야합니다. 그러나 컨트롤러의 유효성 검사가 성공하면 파일을 버킷으로 옮길 필요가 있습니다. 이것을 달성하기 위해 미들웨어를 사용하는 것입니다. $ request-> withAttribute()를 사용해야 할 때조차도 그것을 할 수있는 방법이 있습니까?요청에 withAttribute를 사용하는 컨트롤러 다음에 미들웨어를 실행하는 방법이 있습니까? (슬림 3)

답변

0

예. 그렇습니다. 미들웨어는 호출 가능한 스택의 또 다른 계층입니다.

은 전이나 코드에 정의 된 후 적용 여부 :

<?php 
// Add middleware to your app 
$app->add(function ($request, $response, $next) { 
    $response->getBody()->write('BEFORE'); 
    $response = $next($request, $response); 
    $response->getBody()->write('AFTER'); 

    return $response; 
}); 

$app->get('/', function ($request, $response, $args) { 
    $response->getBody()->write(' Hello '); 

    return $response; 
}); 

$app->run(); 

이 것 출력이 HTTP 응답 본문 :

BEFORE Hello AFTER 

따라서, 귀하의 경우에는 내가 뭔가를 갈 것을 이렇게 :

<?php 
class AfterMiddleware 
{ 
    public function __invoke($request, $response, $next) 
    { 
     // FIRST process by controller 
     $response = $next($request, $response); 

     // You can still access request attributes 
     $attrVal = $request->getAttribute('foo'); 

     // THEN check validation status and act accordingly 

     $isValid = true; 

     if ($isValid) { 
      // Send to the bucket 
     } 
    } 
}