# Adding Routes
Plubo uses Plubo Routes, a small library to add custom routes easily, and without having to worry about rewrite rules, rewrite tags, templates or flushing permalinks every time you need to change something.
There are different types of routes:
- Route (template)
- RedirectRoute
- ActionRoute
- PageRoute
# How to add a new route
You can add new routes in General > Routes.php
, inside the function add_routes
.
add_filter( 'plubo/routes', array($this, 'add_routes') );
public function add_routes( $routes ) {
//Your routes
return $routes;
}
# Basic routes
Basic routes take 3 parameters:
Parameter | Type |
---|---|
Route Path | String |
Template file name | String | Callable |
Config | Array (optional) |
Examples:
$routes[] = new Route('clients/list', 'template_name');
//SAGE 10 example
$routes[] = new Route(
'dashboard/{subpage:slug}',
function($matches) {
$subpage = 'dashboard/' . $matches['subpage'];
return locate_template( app('sage.finder')->locate($subpage) );
},
[
'name' => 'my-route'
]
);
# Available syntax
You can use the format {variable_name:type} with any of the available types:
- number (numbers only)
- word (a-Z only)
- slug (a valid WordPress slug)
- date (yyyy-mm-dd date)
- year (4 digits)
- month (01-12)
- day (01-31)
- digit (single digit 0-9)
- jwt (JWT token)
- ip (IPv4)
Note
You can also use custom regex patterns using the format {variable_name:regex_pattern} like {author:([a-z0-9-]+)}
# Changing general template path
By default, Plubo Routes will search the template inside your theme, but you can use a hook to chenge the default path.
If you use Sage 10, you could add something like this:
add_filter( 'plubo/template', function($template) {
return app('sage.finder')->locate($template);
});
# Custom Actions
Named routes provide a hook to execute your custom actions:
add_action('plubo/route_{route_name}', function($matches) {
//Do something
});