How to register a middleware?

Hi,

is it possible to use the HTTP middlewares in Acorn and if so, how do I register it?

I see that there is a wp acorn make:middleware command which creates a Middleware file, but I don’t know how to register it.

Thank you
Maxime

I’ve created a middleware class for displaying the admin-bar on custom routes:

<?php

declare(strict_types=1);

namespace App\Middleware;

use Closure;
use Illuminate\Http\Request;

class WordPress
{
	/**
	 * Makes sure the 'template_redirect' action is executed when using custom routes.
	 * The 'template_redirect' action is required for displaying the WP_Admin_Bar.
	 */
	public function handle(Request $request, Closure $next)
	{
		do_action('template_redirect');

		return $next($request);
	}
}

To use it, add the middleware to your route like this:

Route::get('/route', 'Path\To\Controllers\Controller@show')->middleware(['web', WordPress::class]);
7 Likes