ExpressionEngine® 3 User Guide

Legacy Documentation

You are using the documentation for version 3.5.17. Go here for the latest version or check here for your available upgrades to the latest version.

CP/URL Service

Simple Example

Building URLs in the Control Panel is a common, but exacting task. We have a CP/URL Service to make building these URLs simple. For example:

$url = ee('CP/URL', 'publish/create/1');

The service automatically takes care of appending the session ID when it is required. For more complex URLs we also provide means of adding arbitrary query string variables using the make method:

$url = ee('CP/URL')->make('publish/edit', array('filter_by_channel' => 1));

Or:

$url = ee('CP/URL', 'publish/edit')
      ->setQueryStringVariable('filter_by_channel', 1);

When to use compile()

The CP/URL object has a magic __toString() method that compiles the object into a string when the object is treated as a string (see: PHP’s documentation on the magic __toString() method for more information). The compile() method exists for those occasions when the object is treated as an object but you need a string instead. As per PHP’s documentation on arrays: “Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.” You will also want to compile the object when you want to JSON encode the URL otherwise you will get a JSON object instead of a string.

For example:

$breadcrumb = array(
  ee('CP/URL', 'addons/settings/fortune_cookie')->compile() => lang('fortune_cookie_management')
);

ee()->javascript->set_global(array(
  'fortune_cookie.autosave.URL' => ee('CP/URL', 'addons/settings/fortune_cookie/autosave/')->compile()
));

CP/URL Service Methods

class EllisLab\ExpressionEngine\Library\CP\URLFactory
EllisLab\ExpressionEngine\Library\CP\URLFactory::make($path, $qs = array(), $cp_url = '', $session_id = NULL)

Makes a URL object.

Parameters:
  • $path (string) – The path of the url (ie. ‘publish/edit/2’)
  • $qs (array) – An associative array of query string variables to append to the rendered URL.
  • $cp_url (string) – The base URL to which all else will be appended (ie. ‘admin.php’)
  • $session_id (string|NULL) – A session ID to append to the rendered URL
Returns:

A URL object

Return type:

URL

EllisLab\ExpressionEngine\Library\CP\URLFactory::makeFromString($url)

Makes a URL object from a string.

Parameters:
  • $url (string) – The URL to be parsed into a URL object
Returns:

A URL object

Return type:

URL

EllisLab\ExpressionEngine\Library\CP\URLFactory::getCurrentUrl()

Makes a URL object representing the requested URL.

Returns:A URL object
Return type:URL
EllisLab\ExpressionEngine\Library\CP\URLFactory::decodeUrl($url)

Decodes a base64 encoded, seralized URL object.

Returns:A URL object
Return type:URL

CP/URL Object Methods

class EllisLab\ExpressionEngine\Library\CP\URL
EllisLab\ExpressionEngine\Library\CP\URL::setQueryStringVariable($key, $value)

Sets a key and value which will become the Query String of the request

Parameters:
  • $key (string) – The name of the query string variable
  • $value (string) – The value of the query string variable
Returns:

$this

Return type:

URL

EllisLab\ExpressionEngine\Library\CP\URL::addQueryStringVariables($values)

Sets a values in bulk which will become the Query String of the request

Parameters:
  • $values (array) – An associative array of keys and values
Returns:

$this

Return type:

URL

EllisLab\ExpressionEngine\Library\CP\URL::compile()

Compiles and returns the URL as a string. Typically this is used when you need to use a URL as an array key, or want to json_encode() a URL.

Returns:string
Return type:The URL
EllisLab\ExpressionEngine\Library\CP\URL::__toString()
When accessed as a string simply complile the URL and return that.
Returns:string
Return type:The URL