Slug
in package
implements
Stringable
Creates slug strings for use in queryless URLs.
Table of Contents
Interfaces
- Stringable
Properties
- $known : array<string|int, mixed>
- $redirect_patterns : array<string|int, mixed>
- $requested : array<string|int, mixed>
- $called_redirect_patterns_hook : bool
- $common_words_regex : string
- $id : int
- $slug : string
- $type : string
Methods
- __construct() : mixed
- Constructor.
- __toString() : string
- Return the slug string.
- create() : self
- Convenience wrapper for constructor.
- getCached() : string
- Attempts to fetch a slug string from the cache.
- setRequested() : void
- Sets self::$requested to the slug in the requested URL.
- build() : void
- Builds $this->slug.
- redirectNoncanonical() : void
- If this slug is for the item requested in the URL, but the URL used the wrong slug, this method issues a redirect to the canonical URL.
Properties
$known
public
static array<string|int, mixed>
$known
= ['board' => [], 'topic' => [], 'member' => [], 'group' => []]
Slugs that we know to be correct.
$redirect_patterns
public
static array<string|int, mixed>
$redirect_patterns
= ['board' => ['board' => '{id}.{start}'], 'topic' => ['topic' => '{id}.{start}'], 'member' => ['action' => 'profile', 'area' => '{area}', 'sa' => '{sa}', 'u' => '{id}'], 'group' => ['action' => 'groups', 'sa' => 'members', 'group' => '{id}']]
Patterns used to build redirection URLs if an incorrect slug was provided in the requested URL. Used by Slug::redirectNoncanonical().
Keys are slug types. Values are sub-arrays containing information for building a query string for the redirection URL.
Within each sub-array, keys are URL query parameter names and values are patterns for creating the URL query parameter values.
The patterns can contain literal text and/or substitution tokens.
Substitution tokens are surrounded by braces. The content inside the braces is usually a $_REQUEST key, with two special cases:
-
The special token
{id}
will be replaced with the slug's $id value. -
The special token
{start}
will be replaced with $_REQUEST['start'] or with0
if $_REQUEST['start'] is not set.
All other tokens will be replaced with the indicated $_REQUEST variable or with an empty string if that $_REQUEST variable is not set.
Once all substitutions have been made, the set of parameters will be filtered. Parameters that are intentionally valueless (i.e. the pattern itself is an empty string) will be retained, but any other valueless parameters will be discarded from the compiled URL query string.
MOD AUTHORS:
-
To add patterns for custom slug types to this array or to adjust the existing patterns, use the integrate_slug_redirect_patterns hook.
-
Redirect patterns use a normal URL query, not a queryless URL. The query will be rewritten to a queryless URL by other code elsewhere.
$requested
public
static array<string|int, mixed>
$requested
The slug in the requested URL, if any.
When applicable, this is set during route parsing.
$called_redirect_patterns_hook
protected
static bool
$called_redirect_patterns_hook
= false
Whether the integrate_slug_redirect_patterns hook has been called yet.
$common_words_regex
protected
static string
$common_words_regex
Regular expression to match common words in the current language.
$id
protected
int
$id
The ID of the item associated with this slug.
$slug
protected
string
$slug
The slug string.
$type
protected
string
$type
The type of the item associated with this slug.
Methods
__construct()
Constructor.
public
__construct(string $string, string $type, int $id[, int $max_length = 30 ]) : mixed
Parameters
- $string : string
-
The original string.
- $type : string
-
Type of the associated item.
- $id : int
-
ID of the associated item.
- $max_length : int = 30
-
Maximum length of the slug string. Default: 30.
__toString()
Return the slug string.
public
__toString() : string
Return values
stringcreate()
Convenience wrapper for constructor.
public
static create(string $string, string $type, int $id[, int $max_length = 30 ]) : self
Parameters
- $string : string
-
The original string.
- $type : string
-
Type of the associated item.
- $id : int
-
ID of the associated item.
- $max_length : int = 30
-
Maximum length of the slug string. Default: 30.
Return values
self —The created object.
getCached()
Attempts to fetch a slug string from the cache.
public
static getCached(string $type, int $id) : string
Parameters
- $type : string
-
Type of the associated item.
- $id : int
-
ID of the associated item.
Return values
string —The cached slug string, or an empty string on failure.
setRequested()
Sets self::$requested to the slug in the requested URL.
public
static setRequested(string $slug, string $type, int $id) : void
Parameters
- $slug : string
-
The slug string provided in the requested URL.
- $type : string
-
Type of the associated item.
- $id : int
-
ID of the associated item.
build()
Builds $this->slug.
protected
build(string $string, int $max_length) : void
Parameters
- $string : string
-
The original string.
- $max_length : int
-
Maximum length of the slug string.
redirectNoncanonical()
If this slug is for the item requested in the URL, but the URL used the wrong slug, this method issues a redirect to the canonical URL.
protected
redirectNoncanonical() : void