■ 백엔드 ■/Slim2018.05.24 11:43

Middleware



Application middleware


<?php
$timer = function($request, $response, $next){
    // before
    $start = microtime(true);

    // call next middleware
    $response = $next($request, $response);

    // after
    $taken = microtime(true) - $start;
    $response->write("<!-- Time taken: $taken -->");

    return $response;
}

$app->add($timer);



Route middleware

<?php
$app->get('/hello/{name}', function(...){...})
    ->add(function($request, $response, $next){

    // before : sanitise route parameter
    $name = strip_tags($request->getAttribute('name'));
    $request = $request->withAttribute('name', $name);

    return $next($request, $response);
});



Leverage middleware


Application level:

Authentication

Navigation

Session


Route level:

Access control

Validation



'■ 백엔드 ■ > Slim' 카테고리의 다른 글

Slim Framework 3 Skeleton Application  (0) 2018.06.25
Twig views  (0) 2018.05.24
Middleware  (0) 2018.05.24
Routes  (0) 2018.05.24
index.php  (0) 2018.05.24
03. 웹서버 설정  (0) 2018.05.23
02. 컴포저로 slim 설치하기  (0) 2018.05.22
01. slim 프레임워크  (0) 2018.05.22
Posted by SAP (Study And Programming) by serpiko

댓글을 달아 주세요