中间件取消路由前的 Silex 应用程序

Silex application before middleware cancel route

我找不到在 "Before Middleware" 中取消操作并在不执行控制器的情况下将响应发送给客户端的方法。

例如:

$app->before( function( Request $request ) {
    // Do something here
    // ...
    // Here sent the response to the client and don't execute the controller
}});

可以吗?

一个例子

这段代码工作正常。我正在寻找使用框架内置方法的另一种解决方案。如果不可能,没问题。

$app->before( function( Request $request ) {
    // Do something here
    // ...

    header( 'Content-Type: application/json' );

    echo json_encode( array(
        'message' => 'Invalid token'
    ) );

    http_response_code( 400 ); // This code return Bad Request to client
    exit; // Cancel the rest of the framework
}});

你检查过 official documentation 了吗?来自中间件文档:

Short-circuiting the Controller

If a before middleware returns a Response object, the request handling is short-circuited (the next middleware won't be run, nor the route callback), and the Response is passed to the after middleware right away:

$app->before(function (Request $request) {
  // redirect the user to the login screen if access to the Resource is protected
  if (...) {
      return new RedirectResponse('/login');
  }
});

如果您 return 一个 Response 对象,它应该可以工作,而不是调用控制器。

如果您想立即取消您的请求并且 return 400 响应使用例外。在您的情况下,用户未经授权,因此适合 401 之类的内容。

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\HttpFoundation\Response;

$app->match('/', function () use ($app) {
    $app->before(function (Request $request) {
        $loggedIn= false;

        if (!$loggedIn) {
             throw new UnauthorizedHttpException(null,'unauthorized',null,Response::HTTP_UNAUTHORIZED);
        }
    });
});

$app->error(function (\Exception $e, Request $request, $code) {
    $message = strlen($e->getMessage()) > 0 ? $e->getMessage() : null;
    switch ($code) {
        case Response::HTTP_UNAUTHORIZED:
            $response = new Response($message, Response::HTTP_UNAUTHORIZED);
            break;
        default:
            $response = new Response($message, Response::HTTP_NOT_FOUND);
            break;
    }
    return $response;
});