Mecha CMS

Deep learning on using the API.

Class URL

Author: Taufik Nurrohman · 82 Views

Tweet

Extract current URL data.

Table of Content

Long 

URL::long($url, $root = true);

Convert relative URL into full URL:

__test__(
    URL::long(""),
    URL::long('foo'),
    URL::long('?foo'),
    URL::long('&foo'),
    URL::long('#foo'),
    URL::long('javascript:;'),
    URL::long('foo/bar'),
    URL::long('//foo'),
    URL::long('http://foo'),
    URL::long('/'),
    URL::long('/foo'),
    URL::long('/?foo'),
    URL::long('/&foo'),
    URL::long('/#foo'),
    URL::long('/javascript:;'),
    URL::long('/foo/bar')
);

Note: If $root is false, then a / character at the beginning of URL path will be ignored, and the result will always relative to the URL path where the CMS is being installed. The $root parameter changes makes sense only when Mecha is installed in a sub–directory.

Result:

[[url.protocol]][[url.host]]/directory
[[url.protocol]][[url.host]]/directory/foo
?foo
&foo
#foo
javascript:;
[[url.protocol]][[url.host]]/directory/foo/bar
//foo
http://foo
[[url.protocol]][[url.host]]/foo
?foo
&foo
#foo
javascript:;
[[url.protocol]][[url.host]]/foo/bar

Short 

URL::short($url, $root = true);

Convert full URL into relative URL:

__test__(
    URL::short('[[url.protocol]][[url.host]]/directory'),
    URL::short('[[url.protocol]][[url.host]]/directory/foo'),
    URL::short('[[url.protocol]][[url.host]]/directory?foo'),
    URL::short('[[url.protocol]][[url.host]]/directory&foo'),
    URL::short('[[url.protocol]][[url.host]]/directory#foo'),
    URL::short('[[url.protocol]][[url.host]]/directory/javascript:;'),
    URL::short('[[url.protocol]][[url.host]]/directory/foo/bar'),
    URL::short('//foo'),
    URL::short('http://foo'),
    URL::short('[[url.protocol]][[url.host]]/directory', false),
    URL::short('[[url.protocol]][[url.host]]/directory/foo', false),
    URL::short('[[url.protocol]][[url.host]]/directory?foo', false),
    URL::short('[[url.protocol]][[url.host]]/directory&foo', false),
    URL::short('[[url.protocol]][[url.host]]/directory#foo', false),
    URL::short('[[url.protocol]][[url.host]]/directory/javascript:;', false),
    URL::short('[[url.protocol]][[url.host]]/directory/foo/bar', false),
    URL::short('//foo', false),
    URL::short('http://foo', false)
);

Note: If $root is false, then the sub–directory path where the CMS is being installed will be removed too, and the leading / will also be removed. The $root parameter changes makes sense only when Mecha is installed in a sub–directory.

Result:

/directory
/directory/foo
/directory?foo
/directory&foo
/directory#foo
/directory/javascript:;
/directory/foo/bar
//foo
http://foo
foo
?foo
&foo
#foo
javascript:;
foo/bar
//foo
http://foo

Initial 

URL::I($url);

Remove the trailing page offset from URL:

__test__(
    URL::I('http://mecha-cms.com/blog'),
    URL::I('http://mecha-cms.com/blog/4')
);

Result:

http://mecha-cms.com/blog
http://mecha-cms.com/blog

Advance 

Constructor 

URL();

Inspect URL properties:

$url = new URL();

echo $url->path; // return the URL path
echo $url->path('#'); // return the URL path if any, otherwise, return `#`
echo $url; // return `$url->url` (the base URL)

The available properties:

// [[url.protocol]][[url.host]]/directory/foo/bar?foo=bar#foo

$url → [[url.protocol]][[url.host]]/directory
 │
 ├── scheme: [[url.scheme]]
 ├── protocol: [[url.protocol]]
 ├── host: [[url.host]]
 ├── port: [[url.port]]
 ├── user: null ← or depends on `$_SESSION['url']['user']`
 ├── pass: null ← or depends on `$_SESSION['url']['pass']`
 ├── directory: directory
 ├── url: [[url.protocol]][[url.host]]/directory
 ├── path: foo/bar
 ├── query: ?foo=bar
 ├── previous: null ← or depends on `$_SESSION['url']['previous']`
 ├── current: [[url.protocol]][[url.host]]/directory/foo/bar
 ├── next: null ← or depends on `$_SESSION['url']['next']`
 └── hash: null ← or depends on `$_COOKIE['url']['hash']`

0 Comments

No comments yet.