Merge pull request #9585 from nextcloud/fix/dev-manual/middleware-docs

fix(dev-manual): Update middleware docs to state of the art
This commit is contained in:
Christoph Wurst
2023-01-25 11:47:47 +01:00
committed by GitHub

View File

@@ -28,15 +28,54 @@ To generate your own middleware, simply inherit from the Middleware class and ov
/**
* this replaces "bad words" with "********" in the output
*/
public function beforeOutput($controller, $methodName, $output) {
public function beforeOutput($controller, $methodName, $output): string {
return str_replace('bad words', '********', $output);
}
}
The middleware can be registered in the :doc:`dependency_injection` and added using the **registerMiddleware** method:
The middleware can be registered in the app's ``Application`` class:
.. code-block:: php
:caption: lib/AppInfo/Application.php
:emphasize-lines: 20
<?php
declare(strict_types=1);
namespace OCA\MyApp\AppInfo;
use OCA\MyApp\Middleware\CensorMiddleware;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
class Application extends App implements IBootstrap {
public function __construct() {
parent::__construct('myapp');
}
public function register(IRegistrationContext $context): void {
$context->registerMiddleware(CensorMiddleware::class);
}
public function boot(IBootContext $context): void {}
}
Dependency Injection Container Registration
-------------------------------------------
.. deprecated:: 20
Middlewares can also be added using the **registerMiddleware** method of the container:
.. code-block:: php
:caption: lib/AppInfo/Application.php
:emphasize-lines: 14-17
<?php
@@ -48,20 +87,10 @@ The middleware can be registered in the :doc:`dependency_injection` and added us
class MyApp extends App {
/**
* Define your dependencies in here
*/
public function __construct(array $urlParams = []) {
parent::__construct('myapp', $urlParams);
$container = $this->getContainer();
/**
* Middleware
*/
$container->registerService(CensorMiddleware::class, function(IServerContainer $c): CensorMiddleware{
return new CensorMiddleware();
});
// executed in the order that it is registered
$container->registerMiddleware(CensorMiddleware::class);
@@ -100,7 +129,7 @@ Sometimes it is useful to conditionally execute code before or after a controlle
/**
* Add custom header if @MyHeader is used
*/
public function afterController($controller, $methodName, Response $response) {
public function afterController($controller, $methodName, Response $response): Response {
if($this->reflector->hasAnnotation('MyHeader')) {
$response->addHeader('My-Header', 3);
}
@@ -108,39 +137,4 @@ Sometimes it is useful to conditionally execute code before or after a controlle
}
}
Now adjust the container to inject the reflector:
.. code-block:: php
<?php
namespace OCA\MyApp\AppInfo;
use OCP\AppFramework\App;
use OCP\IServerContainer;
use OCA\MyApp\Middleware\HeaderMiddleware;
class MyApp extends App {
/**
* Define your dependencies in here
*/
public function __construct(array $urlParams=array()){
parent::__construct('myapp', $urlParams);
$container = $this->getContainer();
/**
* Middleware
*/
$container->registerService(HeaderMiddleware::class, function(IServerContainer $c): HeaderMiddleware {
return new HeaderMiddleware($c->get('ControllerMethodReflector'));
});
// executed in the order that it is registered
$container->registerMiddleware(HeaderMiddleware::class);
}
}
.. note:: An annotation always starts with an uppercase letter