URL helpers of _class('rewrite') (class yf_rewrite) next to url(): compare two URLs, build the
URL of the current page and the home pages, check a link against the current page. They follow the same settings as url():
$PROJECT_CONF['tpl']['REWRITE_MODE'] on the user side, yf_rewrite::$ADMIN_REWRITE_MODE in the admin, the host
of WEB_PATH / WEB_DOMAIN. All methods: API reference / yf_rewrite;
how url() builds links: demo / rewrite urls.
get_current_url
get_current_url($params = []): the canonical URL of the current page, built by url() from the object,
action, id and page that the router left in $_GET. Keys of $params add or replace params, a null
value removes one. The option for_section ('user' or 'admin') picks the side, by default the
current one. On the user side the default page (SITE_DEFAULT_PAGE, else home_page) gives the home page URL.
// on /news/show/5 with REWRITE_MODE (?object=news&action=show&id=5 without)
_class('rewrite')->get_current_url(); // http://example.com/news/5
_class('rewrite')->get_current_url(['id' => null, 'page' => 2]); // http://example.com/news/0/2
_class('rewrite')->get_current_url(['sort' => 'date']); // http://example.com/news/5?sort=date
Only object and action of a-z 0-9 _ - are taken; id and page are taken with every character that would change the link
(& = ? # / %, spaces, quotes, angle brackets, braces) percent-encoded. No other request param and nothing of
$_SERVER goes in, so the URL is safe to build links from. Like any URL, print it with _prepare_html().
is_urls_same
is_urls_same($url1, $url2, $params = []): true when the two URLs are the same after normalising both. These
differences do not count: the case of scheme and host, a trailing dot of the host, the default port (80 for http, 443 for https), a
trailing slash, the order of query params, empty query values, the built-in params of url() (debug,
error, no_cache, no_core_cache, host, get_host) and the
#fragment. A link without a host (/news, ./?object=news) is on the site host; a relative one is
below the site root (in the admin below the admin path); when one URL has no scheme the scheme is not compared.
| Option | Effect |
|---|---|
ignore_scheme | http and https are the same |
ignore_query | only object, action, id, page and task of the query count (the route of the query form) |
ignore_params | a list of more query params that do not count, such as ['utm_source', 'sort'] |
keep_builtins | the built-in params count |
compare_fragment | the #fragment counts |
for_section | 'user' or 'admin': the side whose base a relative link has |
$r = _class('rewrite');
$r->is_urls_same('http://Example.com:80/news/?b=2&a=1', 'http://example.com/news?a=1&b=2'); // true
$r->is_urls_same('http://example.com/news', 'https://example.com/news'); // false
$r->is_urls_same('http://example.com/news', 'https://example.com/news', ['ignore_scheme' => true]); // true
It compares URLs, not routes: with REWRITE_MODE /news/show/5 and /news/5 open the same page
but are different URLs. Build both with url() to compare pages.
is_current_url
is_current_url($url, $params = []): is_urls_same($url, get_current_url(), $params), with the same options.
The current page is its object, action, id and page; a link with more params is the current page only with ignore_query
or ignore_params. For the active item of a menu:
foreach ($items as &$item) {
$item['active'] = _class('rewrite')->is_current_url($item['link'], ['ignore_params' => ['utm_source']]);
}
get_user_home_url, get_admin_home_url
get_user_home_url($params = []): the user home page, url('/') (with REWRITE_MODE and
yf_rewrite::$USE_WEB_PATH it is WEB_PATH), which opens SITE_DEFAULT_PAGE.
get_admin_home_url($params = []): ADMIN_WEB_PATH, the same with or without pretty admin URLs;
false where ADMIN_WEB_PATH is not defined. $params adds query params as url() takes
them: get_user_home_url(['ref' => 'mail']) gives http://example.com/?ref=mail.
_is_our_url
_is_our_url($url): true when $url is the canonical link of a user page for the current
rewrite mode and the site host: parsed by the router and built again with url(), it is the same URL
(is_urls_same()). With REWRITE_MODE /news/5 is canonical and ?object=news&id=5
is not; without it the other way round. A URL of another host, or one that cannot be parsed, is false. It changes no
state of the request.