waRouting

HTTP requests routing management.

Contents...

To obtain an instance of waRouting class and call its public non-static methods, use wa()->getRouting(); e.g.:

wa()->getRouting()->getCurrentUrl();

Methods

  • clearUrl

    Removes special characters from routing rule address.

  • getAliases

    Returns the list of site aliases.

  • getAllRoutes

    Returns the list of all routing rules of all sites.

  • getByApp

    Returns all routing rules set up for the frontend of a specified app.

  • getCurrentUrl

    Returns relative URL of current HTTP request with GET parameters removed.

  • getDomain

    Returns site URL parsed from the URL of current HTTP request, or specified site's URL.

  • getDomains

    Returns array of all sites' URLs.

  • getRootUrl

    Returns root URL of the routing rule matching current HTTP request in frontend.

  • getRoute

    Returns parameters of the routing rule matching current HTTP request in frontend.

  • getRoutes

    Returns array of routing rules of specified or current site.

  • getUrl

    Returns URL for a frontend request generated from specified routing parameters.

  • getUrlByRoute

    Returns root URL for a frontend request for specified routing rule.

  • isAlias

    Returns address of the main site for specified alias.

  • getRuleForUrl

    Returns parameters of the website routing rule which is used to process requests to the specified URL.

public static function clearUrl ($url)

Removes special characters from routing rule address.

Parameters

  • $url

    Routing rule address.

Example

waRouting::clearUrl('site/*');

Result

'site/'

public function getAliases()

Returns the list of site aliases.

Example

wa()->getRouting()->getAliases();

Result

[
  'alias-domain.com' => 'main-domain.com',
  'another-alias-domain.com' => 'main-domain.com',
]

public function getAllRoutes()

Returns the list of all routing rules of all sites.

Example

wa()->getRouting()->getAllRoutes();

public function getByApp ($app_id, $domain = null)

Returns all routing rules set up for the frontend of a specified app.

Parameters

  • $app_id

    ID of the app, whose routing rules must be returned.

  • $domain

    Address of site whose routing rules must be returned. If not specified (by default), then routing rules of all existing sites are returned.

Example 1

wa()->getRouting()->getByApp('shop');

Result

array(
  'domain1.com' => array(
    1 => array(
      'url' => '*',
      'app' => 'shop',
      '_name' => 'Store #1',
      'theme' => 'default',
      'theme_mobile' => 'default',
      'locale' => 'en_US',
      'title' => '',
      'meta_keywords' => '',
      'meta_description' => '',
      'og_title' => '',
      'og_image' => '',
      'og_video' => '',
      'og_description' => '',
      'og_type' => '',
      'url_type' => '1',
      'type_id' => '0',
      'currency' => 'USD',
      'stock_id' => '1',
      'public_stocks' => '0',
      'drop_out_of_stock' => '2',
      'payment_id' => '0',
      'shipping_id' => '0',
    ),
  ),
  'domain2.com' => array(
    3 => array(
      'url' => 'shop/*',
      'app' => 'shop',
      '_name' => 'Store #2',
      'theme' => 'custom',
      'theme_mobile' => 'mobile',
      'locale' => 'en_US',
      'title' => '',
      'meta_keywords' => '',
      'meta_description' => '',
      'og_title' => '',
      'og_image' => '',
      'og_video' => '',
      'og_description' => '',
      'og_type' => '',
      'url_type' => '0',
      'type_id' => '0',
      'currency' => 'USD',
      'stock_id' => '2',
      'public_stocks' => '0',
      'drop_out_of_stock' => '0',
      'payment_id' => '0',
      'shipping_id' => '0',
    ),
  ),
)

Example 2

wa()->getRouting()->getByApp('shop', 'domain1.com')

Result

array(
  'url' => '*',
  'app' => 'shop',
  '_name' => 'Shop #1',
  'theme' => 'default',
  'theme_mobile' => 'default',
  'locale' => 'en_US',
  'title' => '',
  'meta_keywords' => '',
  'meta_description' => '',
  'og_title' => '',
  'og_image' => '',
  'og_video' => '',
  'og_description' => '',
  'og_type' => '',
  'url_type' => '1',
  'type_id' => '0',
  'currency' => 'USD',
  'stock_id' => '1',
  'public_stocks' => '0',
  'drop_out_of_stock' => '2',
  'payment_id' => '0',
  'shipping_id' => '0',
),

public function getCurrentUrl()

Returns relative URL of current HTTP request with GET parameters removed.

Example

$current_url = wa()->getRouting()->getCurrentUrl();

public function getDomain ($domain = null, $check = false, $return_alias = true)

Returns site URL parsed from the URL of current HTTP request, or specified site's URL.

Parameters

  • $domain

    Site address. If not specified (by default), then site address is parsed from the URL of the HTTP request. If site address is specified, then it is returned as specified, without additional processing.

  • $check

    Flag requiring availability of a site with the address parsed from the URL of the HTTP request among the sites set up in framework's routing configuration. If the parsed site address is not found in framework routing configuration, then its main version without www. prefix, or with the prefix, if its missing in the original site address, is searched for.

    If main site address is found as described above, then that address is returned if method is called in backend, or, if in frontend, 301 redirect is performed to the modified URL of the original HTTP request, with the original site address replaced with the main one found in the routing configuration, without the value of $return_alias parameter taken into account.

  • $return_alias

    Flag requiring to return the address of main site if the site address parsed from the URL of the HTTP request is set up as an alias for another, main, site.

Example

$main_site_domain = wa()->getRouting()->getDomain(null, false, true);

public function getDomains()

Returns array of all sites' URLs.

Example

wa()->getRouting()->getDomains();

Result

array(
  0 => 'domain1.com',
  1 => 'domain2.com',
  2 => 'domain3.com',
)

public function getRootUrl()

Returns root URL of the routing rule matching current HTTP request in frontend.

Example

wa()->getRouting()->getRootUrl();

Result

//for routing rule with URL shop/*
'shop/'
    
//for routing rule with URL *
''

public function getRoute ($name = null)

Returns parameters of the routing rule matching current HTTP request in frontend.

Parameters

  • $name

    ID of the routing rule parameter whose value must be returned. If not specified (by default), then method returns the array of all available parameters.

Example 1

wa()->getRouting()->getRoute();

Result

array(
  'url' => 'site/*',
  'app' => 'site',
  'theme' => 'default',
  'theme_mobile' => 'default',
  'locale' => 'en_US',
)

Example 2

wa()->getRouting()->getRoute('theme');

Result

'default'

public function getRoutes ($domain = null)

Returns array of routing rules of specified or current site.

Parameters

  • $domain

    Address of site whose routing rules must be returned. If not specified (by default), then method returns the array of routing rules of the site of the current HTTP request.

Example

wa()->getRouting()->getRoutes();

Result

array(
  3 => array(
    'url' => 'site/*',
    'app' => 'site',
    'theme' => 'default',
    'theme_mobile' => 'default',
    'locale' => 'en_US',
  ),
  2 => array(
    'url' => 'photos/*',
    'app' => 'photos',
    'theme' => 'default',
    'theme_mobile' => 'default',
    'locale' => 'en_US',
    'url_type' => '0',
    'title' => '',
    'meta_keywords' => '',
    'meta_description' => '',
  ),
  1 => array(
    'url' => '*',
    'app' => 'shop',
    'theme' => 'default',
    'theme_mobile' => 'default',
    'locale' => 'en_US',
    'title' => '',
    'meta_keywords' => '',
    'meta_description' => '',
    'og_title' => '',
    'og_image' => '',
    'og_video' => '',
    'og_description' => '',
    'og_type' => '',
    'url_type' => '1',
    'type_id' => '0',
    'currency' => 'USD',
    'stock_id' => '7',
    'public_stocks' => '0',
    'drop_out_of_stock' => '2',
    'payment_id' => '0',
    'shipping_id' => '0',
  ),
)

public function getUrl ($path, $params = array(), $absolute = false, $domain_url = null, $route_url = null)

Returns URL for a frontend request generated from specified routing parameters.

Parameters

  • $path

    App, module, and action IDs separated by slash (/).

  • $params

    Associative array of the following optional parameters:

    • 'domain': domain name specified for one of the websites set up in the framework
    • 'module': module id
    • 'action': action id
    • dynamic URL parameters described in the app's configuration file routing.php for the specified module and action; for example, category_url is such a dynamic parameter in the following routing configuration entry:
      'category/<category_url>/' => 'frontend/category',

    If module and action are specified in the array of optional parameters, then they may be omitted in the first (string) parameter of the method.

  • $absolute

    Flag requiring to return an absolute URL instead of a relative one.

  • $domain_url

    Address of site to generate a URL for.

  • $route_url

    Address of a routing rule to generate a URL for.

Example

wa()->getRouting()->getUrl(
    'shop/frontend/category/<category_url>/',
    array('category_url' => 'sale'),
    true,
    'domain2.com',
    'facebook/*'
);

Result

'http://domain2.com/facebook/category/sale/'

public static function getUrlByRoute ($route, $domain = null)

Returns root URL for a frontend request for specified routing rule.

Parameters

  • $route

    Array of parameters of the routing rule for which a root URL must be generated.

  • $domain

    Address of site for which a URL must be generated.

    If specified, then method returns the absolute root URL for specified routing rule. If not specified (by default), then method returns relative root URL of the routing rule with trailing * character removed.

Example

waRouting::getUrlByRoute(
    array('url' => 'shop/*'),
    'domain.com'
);

Result

'http://domain.com/shop/'

public function isAlias ($domain)

Returns address of the main site for specified alias.

Parameters

  • $domain

    Address of site alias for which the address of its main site must be returned.

Example

wa()->getRouting()->isAlias('alias-domain.com');

Result

'main-domain.com'

public function getRuleForUrl ($routes, $url)

Returns parameters of the website routing rule which is used to process requests to the specified URL.

Parameters

  • $routes

    Array of parameters of routing rules of a certain website from configuration file wa-config/routing.php.

  • $url

    Relative URL (without leading / character) for which a matching routing rule must be found.

Example

$routing = wa()->getRouting();
$domain_routes = $routing->getRoutes('mydomain.com');
$url_route = $routing->getRuleForUrl($domain_routes, 'shop/');
wa_dump($url_route);

Result

[
    'url' => 'shop/*',
    'app' => 'shop',
    'theme' => 'default',
    'theme_mobile' => 'default',
    'checkout_version' => '2',
    'locale' => 'en_US',
    'title' => '',
    'meta_keywords' => '',
    'meta_description' => '',
    'og_title' => '',
    'og_image' => '',
    'og_video' => '',
    'og_description' => '',
    'og_type' => '',
    'og_url' => '',
    'url_type' => '0',
    'products_per_page' => '',
    'type_id' => '0',
    'currency' => 'USD',
    'public_stocks' => '0',
    'drop_out_of_stock' => '0',
    'payment_id' => '0',
    'shipping_id' => '0',
    'checkout_storefront_id' => '41c1fb59db445e1b2aa68102f56e161e',
]