NAME

Catalyst::Action::SubDomain - Match action against names of subdomains

VERSION

Version 0.07

SYNOPSIS

Match subdomain name

sub method : ActionClass('SubDomain') :SubDomain('level,regexp') {
    my ( $self, $c ) = @_;
    ..
}

Get number of domain levels and subdomain name at last level.

sub method : ActionClass('SubDomain') {
    my ( $self, $c ) = @_;
    my $max_level = $c->action->number_of_domains($c);
    my $subdomain = $c->action->domain($c, $max_level);
}

EXAMPLES

Root controller action for main site and subdomain with no more than 3 chars

sub default :Path('/') : ActionClass('SubDomain') : SubDomain('3,^\w{0,3}$') {
    my ( $self, $c ) = @_;
}

Foo controller action for rest subdomains

sub index :Path('/') :ActionClass('SubDomain') :SubDomain('3,^\w{4,}$') {
    my ( $self, $c ) = @_;
}

This example shows that actions will be match only when 3-rd level domain exists and contains alpha-numerical chars (foo123.example.com).

sub index :Path('/') :Args(0) :ActionClass('SubDomain') :SubDomain('3,^\w+$') {
    my ( $self, $c ) = @_;
    $c->response->body('Matched My::App::Controller');
}

foo123.example.com/test

sub test :Path('/test') :ActionClass('SubDomain') :SubDomain('3,^\w+$') {
    my ( $self, $c, @args ) = @_;
    $c->response->body(join('.', map($c->action->domain($c, $_), 1..$c->action->number_of_domains($c))));
}

You can specify more that one subdomain constraint.

sub test :Local :ActionClass('SubDomain') :SubDomain('3,^\w+$') :SubDomain('2,^example$') {
    my ( $self, $c, @args ) = @_;
    my $name = $self->action->domain($c, 3);
}

Note: When combining :ActionClass('SubDomain') with :Chained action you should access action a little bit different way

..
my $action = $c->action->isa('Catalyst::ActionChain')?$c->action->chain->[-1]:$c->action;
my $name = $action->domain($c, 3);
..

METHODS

domain($context, $level)

Returns domain name of specified level. Works only for matched action.

number_of_domains($context)

Get number of domain levels

match

See "METHODS/match" in Catalyst::Action.

INTERNAL METHODS

check_subdomain_constraints

Check subdomains constraints

_cached_domains

Cached domains

AUTHOR

Egor Korablev, <egor.korablev at gmail.com>

COPYRIGHT & LICENSE

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.