URL Parser and Builder

Class Nette\Http\Url encapsulates any URLs according to RFC 3986 and allows easy manipulation with their parts.

The following scheme covers individual parts of URL:

scheme  user  password  host  port  basePath   relativeUrl
   |      |      |        |      |    |             |
 /--\   /--\ /------\ /-------\ /--\/--\/----------------------------\
                     |                     |           |         |
                 authority               path        query    fragment

Usage is then very intuitive:

use Nette\Http\Url;

$url = new Url('http://nette.org/en/documentation?action=history#footer');

echo $url->absoluteUrl; // or directly echo $url; returns full URL
echo $url->scheme;      // http
echo $url->authority;   // nette.org
echo $url->hostUrl;     // http://nette.org
echo $url->path;        // /en/documentation
echo $url->query;       // action=history
echo $url->fragment;    // footer

In addition to http URL scheme, it's possible to use others, for example https, file or ftp.

Parts of an URL can of course be changed.

$url = new Url('http://nette.org/en/documentation?action=history#footer');
$url->path = '/';
echo $url; // http://nette.org/?action=history&page=1#footer

The canonicalize() method converts URL to its canonical form.


Class UrlScript is a descendant of Url. It indicates which part of URL is a path to currently running script. It is used inside Nette\Http\Request class.

                       |              |
                  scriptPath       pathInfo

Related blog posts