Middleware
Middleware sits between a request and a response, allowing you to filter and manipulate HTTP requests. Pollora uses standard Laravel middleware and adds WordPress-specific middleware for seamless integration.
For general Laravel middleware concepts, see the official Laravel documentation.
Pollora Middleware
Section titled “Pollora Middleware”Pollora includes four WordPress-specific middleware that are automatically applied to WordPress routes:
WordPressBindings
Section titled “WordPressBindings”Injects WordPress objects (WP_Post, WP_Query, WP_User, etc.) into route closures and controller methods based on type hints.
// The $post parameter is automatically resolved from the current WordPress contextRoute::wp('single', function (WP_Post $post) { return view('post', compact('post'));});WordPressHeaders
Section titled “WordPressHeaders”Manages HTTP headers for WordPress responses:
- Adds
X-Powered-By: Polloraheader - Controls cache headers
- Removes unnecessary WordPress headers for unauthenticated requests
WordPressBodyClass
Section titled “WordPressBodyClass”Adds route-based CSS classes to the WordPress body_class output, allowing you to style pages based on the matched route condition.
WordPressShutdown
Section titled “WordPressShutdown”Ensures WordPress shutdown hooks (shutdown action, output buffer flushing) are properly executed after the Laravel response is sent.
Creating Custom Middleware
Section titled “Creating Custom Middleware”Use Artisan to generate a new middleware:
php artisan make:middleware CheckWordPressCapabilitynamespace App\Http\Middleware;
use Closure;use Illuminate\Http\Request;
class CheckWordPressCapability{ public function handle(Request $request, Closure $next, string $capability = 'edit_posts') { if (! current_user_can($capability)) { abort(403); }
return $next($request); }}Applying Middleware to WordPress Routes
Section titled “Applying Middleware to WordPress Routes”Route::wp('page', fn () => view('page')) ->middleware('auth');
Route::wp('single', fn () => view('post')) ->middleware(CheckWordPressCapability::class . ':publish_posts');Middleware Groups
Section titled “Middleware Groups”You can group WordPress routes with shared middleware:
Route::middleware(['auth', 'verified'])->group(function () { Route::wp('page', fn () => view('page')); Route::wp('single', fn () => view('post'));});