Manual:Entry point routing
Appearance
MediaWiki version: | ≥ 1.19 |
The PathRouter class can take patterns such as /wiki/$1 and use them to parse query parameters out of REQUEST_URI paths. The WebRequestPathInfoRouter hook can be used to add rules to the path router instance used by WebRequest to handle short ruls.
Usage
[edit]- Matches /wiki/Foo style urls and extracts the title
$router->add( "/wiki/$1" );
- Matches /edit/Foo style urls and sets action=edit
$router->add( [ 'edit' => "/edit/$1" ], [ 'action' => '$key' ] );
- Matches /zh-hant/Foo or /zh-hans/Foo
$router->add( '/$2/$1',
[ 'variant' => '$2' ],
[ '$2' => [ 'zh-hant', 'zh-hans' ] ]
);
- Matches /foo/Bar explicitly and uses "Baz" as the title
$router->addStrict( "/foo/Bar", [ 'title' => 'Baz' ] );
- Matches /help/Foo with "Help:Foo" as the title
$router->add( '/help/$1', [ 'title' => 'Help:$1' ] );
- Matches /Foo and sets 'foo' to 'bar$2' without $2 being replaced
$router->add( '/$1', [ 'foo' => [ 'value' => 'bar$2' ] ] );
- Matches /Foo, adds the key 'foo' with the value 'bar' to the data array and calls functionname( &$matches, $data );
$router->add( '/$1', [ 'data:foo' => 'bar' ], [ 'callback' => 'functionname' ] );
Path patterns
[edit]- Paths may contain $# patterns such as $1, $2, etc...
- $1 will match 0 or more while the rest will match 1 or more
- Unless you use addStrict "/wiki" and "/wiki/" will be expanded to "/wiki/$1"
Parameters
[edit]- In a pattern $1, $2, etc... will be replaced with the relevant contents
- If you used a keyed array as a path pattern, $key will be replaced with the relevant contents
- The default behavior is equivalent to
array( 'title' => '$1' )
- if you don't want the title parameter you can explicitly usearray( 'title' => false )
- You can specify a value that won't have replacements in it using
'foo' => array( 'value' => 'bar' );
Options
[edit]- The option keys $1, $2, etc... can be specified to restrict the possible values of that variable. A string can be used for a single value, or an array for multiple.
- When the option key 'strict' is set (Using addStrict is simpler than doing this directly) the path won't have $1 implicitly added to it.
- The option key 'callback' can specify a callback that will be run when a path is matched. The callback will have the arguments ( &$matches, $data ) and the matches array can be modified.