Registering middleware for `ContactsMenuController`

I’m wanting to register middleware for the OC\Core\Controller\ContactsMenuController (I’m trying to write a filter that limits which contacts a user can see). I’ve been able to register middleware for almost everything else in NC, but I can’t figure this one out.


I believe I figured it out:

namespace OCA\AppName\AppInfo;
use OCA\AppName\Middleware\AppMiddleware;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootstrap;

class Application extends App implements IBootstrap {

	public const APP_ID = 'appname';

	public function __construct(array $urlParams = []) {
		parent::__construct(self::APP_ID, $urlParams);
		$container = \OC::$server->query(\OC\Core\Application::class)->getContainer();

		$container->registerService('AppMiddleware', function($c){
			return new AppMiddleware();


At any rate, it works. Posting this for posterity sake to help others.