$config->setLocation() method

Given a directory to a named location, updates $config->paths and $config->urls for it

  • Paths relative to PW installation root should omit the leading slash, i.e. use site/templates/ and NOT /site/templates/.

  • If specifying just the $dir argument, it updates both $config->paths and $config->urls for it.

  • If specifying both $dir and $url arguments, then $dir refers to $config->paths and $url refers to $config->urls.

  • The $for argument can be: cache, logs, files, tmp, templates, or one of your own. Other named locations may also work, but since they can potentially be used before PW’s “ready” state, they may not be reliable.

  • Warning: anything that changes a system URL may make the URL no longer have the protection of the root .htaccess file. As a result, if you modify system URLs for anything on a live server, you should also update your .htaccess file to reflect your changes (while leaving existing rules for original URL in place).

The following example would be in /site/init.php or /site/ready.php (or equivalent module method). In this example we are changing the location (path and URL) of our /site/templates/ to use a new version of the files in /site/dev-templates/ so that we can test them out with user 'karen', while all other users on the site get our regular templates.

Available since version 3.0.141.

Example

// change templates path and URL to /site/dev-templates/ when user name is 'karen'
if($user->name == 'karen') {
  $config->setLocation('templates', 'site/dev-templates/');
}

Usage

// basic usage
$self = $config->setLocation(string $for, string $dir);

// usage with all arguments
$self = $config->setLocation(string $for, string $dir, $url = '');

Arguments

NameType(s)Description
forstring

Named location from $config->paths or $config->urls, one of: cache, logs, files, tmp, templates, or your own.

dirstring

Directory or URL to the location. Should be either a path or URL relative to current installation root (recommended), or an absolute disk path that resolves somewhere in current installation root. If specifying an absolute path outside of the installation root, then you’ll also want to provide the $url argument since PW won’t know it. You may also specify a blank string for this argument if you only want to set the $url argument.

url (optional)string, bool

If the $dir argument represents both the path and URL relative to site root, you can omit this argument. If path and URL cannot be derived from one another, or you only want to modify the $url (leaving $dir blank), you can specify the URL in this argument. Specify boolean false if you only want to set the $dir (path) and not detect the $url from it.

Return value

self

Exceptions

Method can throw exceptions on error:

  • WireException - If request cannot be accommodated


$config methods and properties

API reference based on ProcessWire core version 3.0.168

Twitter updates

  • Preliminary 2021 roadmap in progress in this week’s update— More
    8 January 2021
  • Happy New Year! Today I’ve bumped the version on the dev branch to 3.0.170, and it’s quite a lot of updates. This post covers most of them. In this post, there’s also a question for you: what would you like to see in ProcessWire in 2021? More
    1 January 2021
  • In this week’s blog post we’ll take a brief look at a powerful new ProFields module for ProcessWire that’s just around the corner—the Combo field: More
    4 December 2020

Latest news

  • ProcessWire Weekly #349
    In the 349th issue of ProcessWire Weekly we're going to cover the latest core and forum updates, introduce some recent ProcessWire resources, and more. Read on!
    Weekly.pw / 16 January 2021
  • ProcessWire 3.0.170 core updates
    Happy New Year! Today I’ve bumped the version on the dev branch to 3.0.170, and it’s quite a lot of updates. This post covers most of them. In this post, there’s also a question for you: what would you like to see in ProcessWire in 2021?
    Blog / 1 January 2021
  • Subscribe to weekly ProcessWire news

“ProcessWire is like a breath of fresh air. So powerful yet simple to build with and customise, and web editors love it too.” —Margaret Chatwin, Web developer