This document is for CakePHP's development version, which can be significantly different from previous releases.
You may want to read current stable release documentation instead.

Url

class Cake\View\Helper\UrlHelper(View $view, array $config = [])

The UrlHelper helps you to generate URLs from your other helpers. It also gives you a single place to customize how URLs are generated by overriding the core helper with an application one. See the Aliasing Helpers section for how to do this.

Generating URLs

Cake\View\Helper\UrlHelper::build($url = null, array $options = [])

Returns a URL pointing to a combination of controller and action. If $url is empty, it returns the REQUEST_URI, otherwise it generates the URL for the controller and action combo. If fullBase is true, the full base URL will be prepended to the result:

echo $this->Url->build([
    'controller' => 'Posts',
    'action' => 'view',
    'bar',
]);

// Output
/posts/view/bar

Here are a few more usage examples:

URL with extension:

echo $this->Url->build([
    'controller' => 'Posts',
    'action' => 'list',
    '_ext' => 'rss',
]);

// Output
/posts/list.rss

URL with prefix:

echo $this->Url->build([
    'controller' => 'Posts',
    'action' => 'list',
    'prefix' => 'Admin',
]);

// Output
/admin/posts/list

URL (starting with ‘/’) with the full base URL prepended:

echo $this->Url->build('/posts', ['fullBase' => true]);

// Output
http://somedomain.com/posts

URL with GET parameters and fragment anchor:

echo $this->Url->build([
    'controller' => 'Posts',
    'action' => 'search',
    '?' => ['foo' => 'bar'],
    '#' => 'first',
]);

// Output
/posts/search?foo=bar#first

The above example uses the ? special key for specifying query string parameters and # key for URL fragment.

URL for named route:

// Assuming a route is setup as a named route:
// $router->connect(
//     '/products/{slug}',
//     [
//         'controller' => 'Products',
//         'action' => 'view',
//     ],
//     [
//         '_name' => 'product-page',
//     ]
// );

echo $this->Url->build(['_name' => 'product-page', 'slug' => 'i-m-slug']);
// Will result in:
/products/i-m-slug

The 2nd parameter allows you to define options controlling HTML escaping, and whether or not the base path should be added:

$this->Url->build('/posts', [
    'escape' => false,
    'fullBase' => true,
]);
Cake\View\Helper\UrlHelper::buildFromPath(string $path, array $params = [], array $options = [])

If you want to use route path strings, you can do that using this method:

echo $this->Url->buildFromPath('Articles::index');
// outputs: /articles

echo $this->Url->buildFromPath('MyBackend.Admin/Articles::view', [3]);
// outputs: /admin/my-backend/articles/view/3

New in version 4.1.0: buildFromPath() was added.

URL with asset timestamp wrapped by a <link rel="preload"/>, here pre-loading a font. Note: The file must exist and Configure::read('Asset.timestamp') must return true or 'force' for the timestamp to be appended:

echo $this->Html->meta([
    'rel' => 'preload',
    'href' => $this->Url->assetUrl(
        '/assets/fonts/your-font-pack/your-font-name.woff2'
    ),
    'as' => 'font',
]);

If you are generating URLs for CSS, Javascript or image files there are helper methods for each of these asset types:

// Outputs /img/icon.png
$this->Url->image('icon.png');

// Outputs /js/app.js
$this->Url->script('app.js');

// Outputs /css/app.css
$this->Url->css('app.css');

// Force timestamps for one method call.
$this->Url->css('app.css', ['timestamp' => 'force']);

// Or disable timestamps for one method call.
$this->Url->css('app.css', ['timestamp' => false]);

Customizing Asset URL generation

If you need to customize how asset URLs are generated, or want to use custom asset cache busting parameters you can use the assetUrlClassName option:

// In view initialize
$this->loadHelper('Url', ['assetUrlClassName' => AppAsset::class]);

When using the assetUrlClassName you must implement the same methods as Cake\Routing\Asset does.

New in version 4.2.0: The assetUrlClassName option was added.

For further information check Router::url in the API.