Download the PHP package rareloop/wp-router without Composer
On this page you can find all versions of the php package rareloop/wp-router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rareloop/wp-router
More information about rareloop/wp-router
Files in rareloop/wp-router
Package wp-router
Short Description Router
License MIT
Homepage https://github.com/rareloop/wp-router
Informations about the package wp-router
This package is no longer supported. Use at your own risk. We recommend using the underlying router: https://github.com/Rareloop/router
Rare WordPress Router
A WordPress wrapper around the Rareloop PHP Router. Easily handle custom endpoints on your WordPress site with this plugin.
Installation
Although not a requirement, using Composer and a setup like Bedrock is the recommended installation method.
Usage
Creating Routes
Map
Creating a route is done using the map
function:
map()
takes 3 parameters:
methods
(array): list of matching request methods, valid values:GET
POST
PUT
PATCH
DELETE
OPTIONS
uri
(string): The URI to match againstaction
(function|string): Either a closure or a Controller string
Route Parameters
Parameters can be defined on routes using the {keyName}
syntax. When a route matches that contains parameters, an instance of the RouteParams
object is passed to the action.
Named Routes
Routes can be named so that their URL can be generated programatically:
If the route requires parameters you can be pass an associative array as a second parameter:
HTTP Verb Shortcuts
Typically you only need to allow one HTTP verb for a route, for these cases the following shortcuts can be used:
Setting the basepath
The router assumes you're working from the route of a domain. If this is not the case you can set the base path:
Controllers
If you'd rather use a class to group related route actions together you can pass a Controller String to map()
instead of a closure. The string takes the format {name of class}@{name of method}
. It is important that you use the complete namespace with the class name.
Example:
Creating Groups
It is common to group similar routes behind a common prefix. This can be achieved using Route Groups: