# Class UrlRuleFactory

Creates a [[UrlRule]]

Creates a [[UrlRule]] from a:

- `string`
- [[UrlMatcher]]
- `RegExp`
- [[StateObject]]

- Defined in router/url/url-rule.js:21
- Defined in router/url/url-rule.js:199

#####
Index

### Constructors

constructor

### Properties

routerGlobals
stateService
urlService

### Methods

create
fromRegExp
fromState
fromUrlMatcher
isUrlRule

##
Constructors

### constructor

- new UrlRuleFactory(urlService, stateService, routerGlobals): UrlRuleFactory

- #### Parameters

  - urlService: any
  - stateService: any
  - routerGlobals: any

  #### Returns UrlRuleFactory

  - Defined in router/url/url-rule.js:22

##
Properties

### routerGlobals

routerGlobals: any

- Defined in router/url/url-rule.js:25

### stateService

stateService: any

- Defined in router/url/url-rule.js:24

### urlService

urlService: any

- Defined in router/url/url-rule.js:23

##
Methods

### create

- create(what, handler): BaseUrlRule

- #### Parameters

  - what: any
  - handler: any

  #### Returns BaseUrlRule

  - Defined in router/url/url-rule.js:34

### fromRegExp

- fromRegExp(regexp, handler): BaseUrlRule & {
  regexp: any;
  type: string;
  }

- A UrlRule which matches based on a regular expression

  The `handler` may be either a [[UrlRuleHandlerFn]] or a string.

  ## Handler as a function

  If `handler` is a function, the function is invoked with:

  - regexp match array (from `regexp`)
  - url: the current Url ([[UrlParts]])
  - router: the router object ([[UIRouter]])

  #### Example:

      var rule = factory.fromRegExp(/^/foo/(bar|baz)$/, match => "/home/" + match[1])
      var match = rule.match('/foo/bar'); // results in [ '/foo/bar', 'bar' ]
      var result = rule.handler(match); // '/home/bar'

  ## Handler as string

  If `handler` is a string, the url is replaced by the string when the Rule is invoked. The string is first interpolated using `string.replace()` style pattern.

  #### Example:

      var rule = factory.fromRegExp(/^/foo/(bar|baz)$/, "/home/$1")
      var match = rule.match('/foo/bar'); // results in [ '/foo/bar', 'bar' ]
      var result = rule.handler(match); // '/home/bar'

  #### Parameters

  - regexp: any
  - handler: any

  #### Returns BaseUrlRule & {
  regexp: any;
  type: string;
  }

  - Defined in router/url/url-rule.js:176

### fromState

- fromState(stateOrDecl, stateService, globals): BaseUrlRule & {
  matchPriority: ((params: any) => number);
  type: string;
  urlMatcher: any;
  } & {
  state: any;
  type: string;
  }

- A UrlRule which matches a state by its url

  #### Example:

      var rule = factory.fromState($state.get('foo'), router);
      var match = rule.match('/foo/123/456'); // results in { fooId: '123', barId: '456' }
      var result = rule.handler(match);
      // Starts a transition to 'foo' with params: { fooId: '123', barId: '456' }

  #### Parameters

  - stateOrDecl: any
  - stateService: any
  - globals: any

  #### Returns BaseUrlRule & {
  matchPriority: ((params: any) => number);
  type: string;
  urlMatcher: any;
  } & {
  state: any;
  type: string;
  }

  - Defined in router/url/url-rule.js:121

### fromUrlMatcher

- fromUrlMatcher(urlMatcher, handler): BaseUrlRule & {
  matchPriority: ((params: any) => number);
  type: string;
  urlMatcher: any;
  }

- A UrlRule which matches based on a UrlMatcher

  The `handler` may be either a `string`, a [[UrlRuleHandlerFn]] or another [[UrlMatcher]]

  ## Handler as a function

  If `handler` is a function, the function is invoked with:

  - matched parameter values ([[RawParams]] from [[UrlMatcher.exec]])
  - url: the current Url ([[UrlParts]])
  - router: the router object ([[UIRouter]])

  #### Example:

      var urlMatcher = $umf.compile("/foo/:fooId/:barId");
      var rule = factory.fromUrlMatcher(urlMatcher, match => "/home/" + match.fooId + "/" + match.barId);
      var match = rule.match('/foo/123/456'); // results in { fooId: '123', barId: '456' }
      var result = rule.handler(match); // '/home/123/456'

  ## Handler as UrlMatcher

  If `handler` is a UrlMatcher, the handler matcher is used to create the new url. The `handler` UrlMatcher is formatted using the matched param from the first matcher. The url is replaced with the result.

  #### Example:

      var urlMatcher = $umf.compile("/foo/:fooId/:barId");
      var handler = $umf.compile("/home/:fooId/:barId");
      var rule = factory.fromUrlMatcher(urlMatcher, handler);
      var match = rule.match('/foo/123/456'); // results in { fooId: '123', barId: '456' }
      var result = rule.handler(match); // '/home/123/456'

  #### Parameters

  - urlMatcher: any
  - handler: any

  #### Returns BaseUrlRule & {
  matchPriority: ((params: any) => number);
  type: string;
  urlMatcher: any;
  }

  - Defined in router/url/url-rule.js:86

### `Static` isUrlRule

- isUrlRule(obj): boolean

- #### Parameters

  - obj: any

  #### Returns boolean

  - Defined in router/url/url-rule.js:199