Middlewares

Middleware is logic that is run before and after each request and is modelled after Django’s Middleware system. It offers the following hooks:

  • beforeController: This is executed before a controller method is being executed. This allows you to plug additional checks or logic before that method, like for instance security checks
  • afterException: This is being run when either the beforeController method or the controller method itself is throwing an exception. The middleware is asked in reverse order to handle the exception and to return a response. If the middleware can’t handle the exception, it throws the exception again
  • afterController: This is being run after a successful controller method call and allows the manipulation of a Response object. The middleware is run in reverse order
  • beforeOutput: This is being run after the response object has been rendered and allows the manipulation of the outputted text. The middleware is run in reverse order

To generate your own middleware, simply inherit from the Middleware class and overwrite the methods that should be used.

  1. <?php
  2. namespace OCA\MyApp\Middleware;
  3. use \OCP\AppFramework\Middleware;
  4. class CensorMiddleware extends Middleware {
  5. /**
  6. * this replaces "bad words" with "********" in the output
  7. */
  8. public function beforeOutput($controller, $methodName, $output){
  9. return str_replace('bad words', '********', $output);
  10. }
  11. }

The middleware can be registered in the container and added using the registerMiddleware method:

  1. <?php
  2. namespace OCA\MyApp\AppInfo;
  3. use \OCP\AppFramework\App;
  4. use \OCA\MyApp\Middleware\CensorMiddleware;
  5. class MyApp extends App {
  6. /**
  7. * Define your dependencies in here
  8. */
  9. public function __construct(array $urlParams=array()){
  10. parent::__construct('myapp', $urlParams);
  11. $container = $this->getContainer();
  12. /**
  13. * Middleware
  14. */
  15. $container->registerService('CensorMiddleware', function($c){
  16. return new CensorMiddleware();
  17. });
  18. // executed in the order that it is registered
  19. $container->registerMiddleware('CensorMiddleware');
  20. }
  21. }

Note

The order is important! The middleware that is registered first gets run first in the beforeController method. For all other hooks, the order is being reversed, meaning: if a middleware is registered first, it gets run last.

Parsing annotations

Sometimes it is useful to conditionally execute code before or after a controller method. This can be done by defining custom annotations. An example would be to add a custom authentication method or simply add an additional header to the response. To access the parsed annotations, inject the ControllerMethodReflector class:

  1. <?php
  2. namespace OCA\MyApp\Middleware;
  3. use \OCP\AppFramework\Middleware;
  4. use \OCP\AppFramework\Utility\IControllerMethodReflector;
  5. use \OCP\AppFramework\Http\Response;
  6. class HeaderMiddleware extends Middleware {
  7. private $reflector;
  8. public function __construct(IControllerMethodReflector $reflector) {
  9. $this->reflector = $reflector;
  10. }
  11. /**
  12. * Add custom header if @MyHeader is used
  13. */
  14. public function afterController($controller, $methodName, Response $response){
  15. if($this->reflector->hasAnnotation('MyHeader')) {
  16. $response->addHeader('My-Header', 3);
  17. }
  18. return $response;
  19. }
  20. }

Now adjust the container to inject the reflector:

  1. <?php
  2. namespace OCA\MyApp\AppInfo;
  3. use \OCP\AppFramework\App;
  4. use \OCA\MyApp\Middleware\HeaderMiddleware;
  5. class MyApp extends App {
  6. /**
  7. * Define your dependencies in here
  8. */
  9. public function __construct(array $urlParams=array()){
  10. parent::__construct('myapp', $urlParams);
  11. $container = $this->getContainer();
  12. /**
  13. * Middleware
  14. */
  15. $container->registerService('HeaderMiddleware', function($c){
  16. return new HeaderMiddleware($c->query('ControllerMethodReflector'));
  17. });
  18. // executed in the order that it is registered
  19. $container->registerMiddleware('HeaderMiddleware');
  20. }
  21. }

Note

An annotation always starts with an uppercase letter