What does `withRouting(wordpress: true)` do when booting Acorn 5?

I’m in the process of upgrading a site to Acorn 5. I’ve read all the docs on how to properly boot Acorn and register routes, and I’m confused by what’s going on in the withRouting() method.

Here’s the example given under Advanced Booting in Installing Acorn in WordPress | Acorn Docs | Roots

Application::configure()
	->withProviders([
		//...
	])
	->withRouting(
		web: base_path('routes/web.php'),    // Laravel-style web routes
		api: base_path('routes/api.php'),    // API routes
		wordpress: true                      // Enable WordPress request handling
	)
	->boot();

I understand the web parameter. I’m a little unclear about api (those are Laravel API routes, entirely separate from the WP REST API, right? — no authentication by default, but I can use Laravel’s API stuff if I want?)

But I have no idea what wordpress does. The mentions of it I found in the docs and here on discourse only note that it’s necessary for Livewire to work.

Tracing the source just leads me to a method Application->handleWordPressRequests(). What does this ultimately DO? Does it have side-effects? Does it change anything with query or template resolution?

Like the method implies, it allows you to handle WP requests with Laravel functionality like middleware.

An example would be adding CSP headers through the GitHub - spatie/laravel-csp: Set content security policy headers in a Laravel app package and flashing GTM data with https://github.com/spatie/laravel-googletagmanager .

<?php
use Roots\Acorn\Application;
use Roots\Acorn\Configuration\Middleware;
use Spatie\Csp\AddCspHeaders;
use Spatie\GoogleTagManager\GoogleTagManagerMiddleware;

add_action('after_setup_theme', function () {
    Application::configure()
        ->withProviders([
            //...
        ])
        ->withMiddleware(function (Middleware $middleware) {
            $middleware->append(AddCspHeaders::class);
            $middleware->append(GoogleTagManagerMiddleware::class);
        })
        ->withRouting(
            wordpress: true,
            web: base_path('routes/web.php'),
        )
        ->boot();
}, 0);

It doesn’t have any side effects, unless you implement something that adds some (like above) :slight_smile:

4 Likes

Middleware! Of course :man_facepalming: Thank you, perfect examples… that could be very useful!