Skip to content

Routing

Lukman Nakib edited this page May 30, 2026 · 1 revision

Routing

All HTTP-shaped entry points are declared in app/routes.php through the Router — one line each. Names are the contract; URLs and admin-ajax action slugs are implementation details.

Declare routes

use WPPluginMatrix\Core\Router;
use WPPluginMatrix\Http\Controllers\BookController;

// REST — wp-json/wp-plugin-matrix/v1/...
Router::rest('books.index',  'GET',  '/books', [BookController::class, 'index']);
Router::rest('books.update', 'POST', '/books/(?P<id>\d+)', [BookController::class, 'update']);

// AJAX — wp-admin/admin-ajax.php
Router::ajax('books.stats', 'GET', [BookController::class, 'stats']);
  • Signature: Router::rest($name, $method, $path, $handler) and Router::ajax($name, $method, $handler).
  • Handlers are first-class callables [Controller::class, 'method'] — your editor can resolve and rename them. (Closures and 'Controller@method' strings also work.)
  • Default capability is manage_options; pass a 5th/4th arg to change it.

Localized Routes (the JS bridge)

The framework emits every registered route's URL + a nonce as window.wppmRoutes. The Vue side reads it through resources/js/admin/Bits/AppMixins.jsnever hand-build an admin-ajax.php?action=… URL:

this.$get('books.stats')                       // AJAX GET
this.$post('books.store', { title: 'Dune' })   // AJAX POST
this.$rest('GET', 'books.index', { page: 1 })  // REST GET
this.$rest('PUT', 'books.update', body, { id })// REST with path param

Nonces (important)

Two nonce contexts, handled for you:

  • AJAX routes verify _wpnonce against the wp_plugin_matrix action (sent as nonce).
  • REST routes use WordPress cookie auth, which validates X-WP-Nonce against the wp_rest action — so Localized Routes also expose a separate restNonce, and the JS sends it for REST calls.

Requests are rebased to the current origin (window.location.origin) so they stay same-origin and authenticated even when the admin is browsed on a different host than the configured WordPress Address.

Generate a route

wp matrix:make:route --method=GET --path=books --controller=Book --action=index
wp matrix:make:types        # regenerate RouteNames.php + wppmRoutes.d.ts for editor autocomplete

Clone this wiki locally