GX::Route::Dynamic

Module Version: 0.2000_01

NAME

GX::Route::Dynamic - Dynamic route class

SYNOPSIS

# Load the class
use GX::Route::Dynamic;

# Create a route object
$route = GX::Route::Dynamic->new(
action => $application->action( 'Blog', 'show' ),
host => 'myblog.com',
path => '/posts/{id:\d+}'
);

DESCRIPTION

This module provides the GX::Route::Dynamic class which extends the GX::Route class.

METHODS

Constructor

new

Returns a new GX::Route::Dynamic object.

$route = GX::Route::Dynamic->new( %attributes );
Attributes:
  • action ( GX::Action object ) [ required ]

    The associated action.

  • constraints ( HASH reference )

    A reference to a hash with constraints for the route's dynamic parts.

  • defaults ( HASH reference )

    A reference to a hash with default values for the route's dynamic parts.

  • host ( string )

    The hostname pattern to bind the route to. If omitted, the route will match any hostname.

  • is_reversible ( bool )

    A boolean flag indicating whether the route is reversible or not. Defaults to true.

  • methods ( ARRAY reference )

    A reference to an array with the names of the HTTP methods to bind the route to. If omitted, the route will match any method.

  • path ( string )

    The path pattern to bind the route to. If omitted, the route will match any path. Trailing slashes are significant.

  • schemes ( ARRAY reference )

    A reference to an array with the URI schemes to bind the route to. If omitted, the route will match any scheme.

Internal attributes:
  • host_regex ( Regexp )
  • host_variables ( ARRAY reference )
  • methods_regex ( Regexp )
  • path_regex ( Regexp )
  • path_variables ( ARRAY reference )
  • reverse_host ( string )
  • reverse_host_variables ( ARRAY reference )
  • reverse_path ( string )
  • reverse_path_variables ( ARRAY reference )
  • reverse_scheme ( string )
  • schemes_regex ( Regexp )
Returns:
Exceptions:

Public Methods

action

Returns the associated action.

$action = $route->action;
Returns:

construct_path

Constructs the path portion of an URI that would match the route.

$path = $route->construct_path( %parameters );
Arguments:
  • %parameters ( named list )

    Values for the dynamic parts of the path.

Returns:
  • $path ( string )
Exceptions:

Also see Example #4 below.

construct_uri

Constructs an URI that would match the route.

$uri = $route->construct_uri( %arguments );
Arguments:
  • fragment ( string )

    The fragment identifier of the URI.

  • host ( string )

    The hostname to use as the authority component of the URI. Defaults to the reverse_host attribute.

  • parameters ( HASH reference )

    A reference to a hash with values for the dynamic parts of the URI.

  • path ( string )

    The path portion of the URI. Defaults to the reverse_path attribute.

  • port ( integer )

    The port number to append to the hostname.

  • query ( string )

    The query component of the URI.

  • scheme ( string )

    The scheme part of the URI. Defaults to the reverse_scheme attribute. "http" is assumed as a fallback.

Returns:
  • $uri ( string )
Exceptions:

Also see Example #4 below.

is_reversible

Returns true if the route is reversible, otherwise false.

$result = $route->is_reversible;
Returns:
  • $result ( bool )

match

Returns a GX::Route::Match object if the route matches, otherwise undef.

$result = $route->match( $context );
Arguments:
Returns:

Internal Methods

constraints

Internal method.

%constraints = $route->constraints;
Returns:
  • %constraints ( named list )

defaults

Internal method.

%defaults = $route->defaults;
Returns:
  • %defaults ( named list )

host

Internal method.

$host = $route->host;
Returns:
  • $host ( string | undef )

host_regex

Internal method.

$host_regex = $route->host_regex;
Returns:
  • $host_regex ( Regexp | undef )

host_variables

Internal method.

@host_variables = $route->host_variables;
Returns:
  • @host_variables ( strings )

methods

Internal method.

@methods = $route->methods;
Returns:
  • @methods ( strings )

methods_regex

Internal method.

$methods_regex = $route->methods_regex;
Returns:
  • $methods_regex ( Regexp | undef )

path

Internal method.

$path = $route->path;
Returns:
  • $path ( string | undef )

path_regex

Internal method.

$path_regex = $route->path_regex;
Returns:
  • $path_regex ( Regexp | undef )

path_variables

Internal method.

@path_variables = $route->path_variables;
Returns:
  • @path_variables ( strings )

reverse_host

Internal method.

$reverse_host = $route->reverse_host;
Returns:
  • $reverse_host ( string | undef )

reverse_host_variables

Internal method.

@reverse_host_variables = $route->reverse_host_variables;
Returns:
  • @reverse_host_variables ( strings )

reverse_path

Internal method.

$reverse_path = $route->reverse_path;
Returns:
  • $reverse_path ( string | undef )

reverse_path_variables

Internal method.

@reverse_path_variables = $route->reverse_path_variables;
Returns:
  • @reverse_path_variables ( strings )

reverse_scheme

Internal method.

$reverse_scheme = $route->reverse_scheme;
Returns:
  • $reverse_scheme ( string | undef )

schemes

Internal method.

@schemes = $route->schemes;
Returns:
  • @schemes ( strings )

schemes_regex

Internal method.

$schemes_regex = $route->schemes_regex;
Returns:
  • $schemes_regex ( Regexp | undef )

USAGE

Examples

Example #1

Host patterns:

host => 'myblog.com'
host => 'myblog.com:80'
host => 'myblog.{domain:com|org}'
host => 'myblog.*'
host => '{author:\w+}.myblog.com'

Example #2

Path patterns:

path => '/posts/{id}'
path => '/posts/{id:\d+}'
path => '/posts/{id:\d+}.{format:html|xml}'
path => '/posts/{year}/{month}/{day}'
path => '/posts/{year:\d{4}}/{month:\d{2}}/{day:\d{2}}'
path => '/posts/*/{id}'

Example #3

Using the constraints option:

$route = GX::Route::Dynamic->new(
action => $application->action( 'Posts', 'show_by_month' ),
path => '/posts/{year}/{month}',
constraints => { 'year' => '\d{4}', 'month' => '\d{2}' }
);

Example #4

Path / URI construction:

$route = GX::Route::Dynamic->new(
action => $application->action( 'Posts', 'show' ),
path => '/posts/{id:\d+}.{format:html|xml}',
defaults => { 'format' => 'html' }
);

$path = $route->construct_path( 'id' => '123' );
# $path is '/posts/123.html'

$path = $route->construct_path( 'id' => '123', 'format' => 'xml' );
# $path is '/posts/123.xml'

$uri = $route->construct_uri( host => 'myblog.com', parameters => { 'id' => '123' } );
# $uri is 'http://myblog.com/posts/123.html'

AUTHOR

Jörg A. Uzarek <uzarek@runlevelnull.de>

COPYRIGHT AND LICENSE

Copyright © 2009-2011 Jörg A. Uzarek.

This module is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License Version 3 as published by the Free Software Foundation.