lithium\net\http\Router::attach()

public static method

Defines a scope and attaches it to a mount point.

Example 1:

Router::attach('app', [
    'absolute' => true,
    'host' => 'localhost',
    'scheme' => 'http://',
    'prefix' => 'web/tests'
]);

Example 2:

Router::attach('app', [
    'absolute' => true,
    'host' => '{:subdomain:[a-z]+}.{:hostname}.{:tld}',
    'scheme' => '{:scheme:https://}',
    'prefix' => ''
]);

Attach the variables to populate for the app scope.

Router::attach('app', null, [
    'subdomain' => 'www',
    'hostname' => 'li3',
    'tld' => 'me'
]);

When using scoped routes, just the scope must be given to match a route. The library is already derived from the scope for you. This way you can keep route definitions short and sweet while still using scopes.

By default the library is directly derived from the scope name. So that all routes attached to an 'app' scope are attached to a library of the same name (i.e. the 'app' library in this case).

This behavior can be overridden like so:

Router::attach('app', ['library' => 'foo']);

Or disable it and continue to use scope and library name in route definitions:

Router::attach('app', ['library' => false]);

Parameters

  • string Name

    of the scope.

  • mixed Settings

    of the mount point or null for setting only variables to populate.

  • array Variables

    to populate for the scope.

Source

	public static function attach($name, $config = null, array $vars = []) {
		if ($name === false) {
			return null;
		}

		if (!isset(static::$_scopes)) {
			static::_initScopes();
		}

		if ($config === null) {
			if ($vars && ($config = static::$_scopes->get($name))) {
				$config['values'] = $vars;
				static::$_scopes->set($name, $config);
			}
			return;
		}

		if (is_array($config) || $config === false) {
			static::$_scopes->set($name, $config);
		}
	}