Sef Route Matcher
Matches incoming SEF paths to route types, IDs, and slugs.
Function signature
ogMatchSefRoute(request_path, routes = array())
Categories
- File and Upload Safety
Parameters
request_pathIncoming request path to match or canonicalize.routesApproved route definition rows used for SEF path matching.Return value
Public-safe status string returned by the function for controller branching or logging.
- success
- message
- data
Compatibility
Existing function name, slug, path, and call order preserved; advertised metadata corrected to the actual source behavior.
Minimum PHP version: 7.4
Security notes
Use caller-owned allowlists and context-specific escaping; validate file paths, routes, email tokens, cart totals, discount rules, and tax-region rules before production use.
Code
<?php
/*
* Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
* Free for personal and internal use. Paid project use requires visible credit
* to Jeffery L. Paris. Corporate use requires a paid license fee unless a
* separate written license states otherwise.
*/
/**
* Matches incoming SEF paths to route types, IDs, and slugs.
*
* Primary use case: Front controller or rewrite target parsing.
* Typical inputs: request path, route definitions.
* Typical output: route match object.
*
* Implementation note: Keep route definitions explicit and ordered.
*
* @param string $request_path Incoming request path without the domain.
* @param array $routes Ordered route definitions with name, pattern, and defaults keys.
* @return array Structured route match data.
*/
function ogMatchSefRoute($request_path, $routes = array()) {
$result = array(
'success' => false,
'message' => '',
'data' => array()
);
$request_path = trim((string)$request_path);
$request_path = parse_url($request_path, PHP_URL_PATH);
$request_path = trim((string)$request_path, '/');
if (!is_array($routes)) {
$result['message'] = 'Route definitions must be an array.';
return $result;
}
if (empty($routes)) {
$routes = array(
array('name' => 'home', 'pattern' => ''),
array('name' => 'list', 'pattern' => '^(snippets|functions|objects|scripts|resources|portfolio|turnkey)$'),
array('name' => 'detail', 'pattern' => '^(snippet|function|object|script|resource|portfolio|turnkey)/([0-9]+)/([a-z0-9-]+)$')
);
}
foreach ($routes as $route) {
if (empty($route['name'])) {
continue;
}
if (!isset($route['pattern'])) {
continue;
}
$pattern = (string)$route['pattern'];
if ($pattern === '' && $request_path === '') {
$result['success'] = true;
$result['message'] = 'SEF route matched.';
$result['data'] = array(
'name' => $route['name'],
'matches' => array(),
'route' => $route
);
return $result;
}
$matches = array();
if (@preg_match('#' . $pattern . '#', $request_path, $matches)) {
if (!empty($matches)) {
$result['success'] = true;
$result['message'] = 'SEF route matched.';
$result['data'] = array(
'name' => $route['name'],
'matches' => $matches,
'route' => $route
);
return $result;
}
}
}
$result['message'] = 'No SEF route matched.';
$result['data'] = array(
'path' => $request_path,
'matches' => array()
);
return $result;
}