The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Eidolon::Driver::Router::Consequent - consequent request router for Eidolon.

SYNOPSIS

Somewhere in application controllers:

    my $r = Eidolon::Core::Registry->get_instance;
    my $router = $r->loader->get_object("Eidolon::Driver::Router::Consequent");

    print "Controller: " . $router->{"controller"}              . "\n";
    print "Handler: "    . $router->{"handler"}                 . "\n";
    print "Parameters: " . join(", ", @{ $router->{"params"} }) . "\n";

DESCRIPTION

The Eidolon::Driver::Router::Consequent driver finds handler for each user request. Routing is based on application "Routing table" in application configuration.

Routing flow

Each row of routing table is sequentally checked if it matches a GET-request string. The router keeps checking rows one by one till the matching one is found, otherwise "DriverError::Router::NotFound" in Eidolon::Driver::Router::Exceptions exception is thrown. If a regular expression in the routing table contains elements enclosed in round braces (), these elements assumed to be handler parameters.

After handler was found, router checks its security policy. If application was defined as a private in configuration (see Eidolon::Core::Config for more information) and a routing rule isn't marked as public - "DriverError::Router::Forbidden" in Eidolon::Driver::Router::Exceptions exception is thrown. Also, this exception is thrown if a handler has got a private mark, without application configuration dependencies.

Routing table

The routing table must be defined in application configuration (Eidolon::Core::Config) hash using {"routes"} key. For example, this could be in your application's lib/Example/Config.pm file:

    package Example;

    use base Eidolon::Core::Config;
    use warnings;
    use strict;

    our $VERSION  = "0.01";

    sub new
    {
        my ($class, $name, $type, $self);

        ($class, $name, $type) = @_;

        $self = $class->SUPER::new($name, $type);

        # ...

        # application routing table
        $self->{"routes"} =
        {
            "/"          => [ "Example::Controller::Default"                      ],
            "news"       => [ "Example::Controller::News"                         ],
            "news/(\d+)" => [ "Example::Controller::News",   "entry"              ],
            "admin"      => [ "Example::Controller::Admin",  "default", "private" ]
        };

        return $self;
    }

As you can see from example below, the routing table is a hash reference, and each key-value pair of it represents a single routing rule.

  • Key

    Key is a usual regular expression without leading and trailing delimiters. "/" key stands for the application root page. As was told before, if a key contains elements enclosed in round braces (), these elements assumed to be handler parameters.

  • Value

    Value is an array reference. It has the following items:

    1. Controller name

    The full name of the controller's package. This item is mandatory.

    2. Handler function name

    The name of the function in the controller's package. This item is optional. If it is omitted, "default" function will be called.

    3. Security attribute

    String, containing public or private value. If value is private, the page will require user authentication for view (in this case you must have user driver loaded, see Eidolon::Driver::User for more information). This array item is optional. If it's omitted, the routing rule assumed to be public.

METHODS

new()

Inherited from "new()" in Eidolon::Driver::Router.

find_handler()

Implementation of abstract method from "find_handler()" in Eidolon::Driver::Router.

get_handler()

Inherited from "get_handler()" in Eidolon::Driver::Router.

get_params()

Inherited from "get_params()" in Eidolon::Driver::Router.

SEE ALSO

Eidolon, Eidolon::Applicaton, Eidolon::Driver::Router

LICENSE

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

AUTHOR

Anton Belousov, <abel@cpan.org>

COPYRIGHT

Copyright (c) 2009, Atma 7, http://www.atma7.com