NAME

Egg::Dispatch::Standard - Dispatch of Egg standard.

SYNOPSIS

package MyApp::Dispatch;
use base qw/ Egg::Dispatch::Standard /;
  
# If the attribute is applied to the key, it sets it with ARRAY by using the 
# refhash function.
Egg->dispatch_map( refhash(

# The content of ARRAY of the key from the left. 'Action name', 'Permission 
# -method', 'Title name'
# * When 0 is set, the permission method passes everything.
[qw/ _default 0 /, 'index page.']=> sub {
  my($e, $dispatch)= @_;
  $e->template('document/default.tt');
  },

# The second element of key ARRAY is set to one when permitting only at 'GET'
# request.
[qw/ bbs_view 1 /, 'BBS']=> sub {
  my($e, $dispatch)= @_;
  .... bbs view code.
  },

# The second element of key ARRAY is set to two when permitting only at 'POST'
# request.
[qw/ bbs_post 2 /, 'Contribution.']=> sub {
  .... bbs post code.
  },

# Empty CODE decides the template from the list of the action name that becomes
# a hit. In this case, it is 'help.tt'.
help => sub { },

[qw/ blog 0 /, 'My BLOG']=>

  # The refhash function for remembrance' sake when you use ARRAY for the key.
  refhash(

  # Prior processing can be defined by '_begin'.
  _begin => sub {
    my($e, $dispatch)= @_;
    ... blog begin code.
    },

  # The regular expression can be used for the action. A rear reference is the
  # third argument over CODE.
  [qr{^article_(\d{4}/\d{2}/\d{2})}, 0, 'Article']=> sub {
    my($e, $dispatch, $parts)= @_;
    ... data search ( $parts->[0] ).
    },

  # A rear reference for a shallower hierarchy extracts the value of
  # $e->dispatch->parts with 'tied'.
  qr{^[A-Z]([a-z0-9]+)}=> {
    qr{^User([A-Z][a-z0-9_]+)}=> {
    my($e, $dispatch, $match)= @_;
    my $low_match= tied($dispatch->parts->[0])->[2];
    ... other code.
    },

  [qw/ edit 0 /, 'BLOG Edit Form.']=> sub {
    my($e, $dispatch)= @_;
    ... edit code.
    },

  # Processing can be defined by '_end' after the fact.
  _end => sub {
    my($e, $dispatch)= @_;
    ... blog begin code.
    },

  # Time when 11 dispatch is set can be saved if it combines with $e->snip2template.
  # Refer to L<Egg::Util> for 'snip2template' method.
  help => {
    _default=> sub {},
    qr{^[a-z][a-z0-9_]+}=> sub {
      my($e, $dispatch)= @_;
      $e->snip2template(1) || return $e->finished('404 Not Found.');
      },
    },

  ),

  ) );

DESCRIPTION

It is dispatch of the Egg standard.

Dispatch is processed according to the content defined in 'dispatch_map'.

Dipatti of the layered structure is treatable.

The value of the point where the action the final reaches should be CODE reference.

Objec of the project and the handler object of dispatch are passed for the CODE reference.

It corresponds to the key to the ARRAY form by using the refhash function. see Tie::RefHash.

Page title corresponding to the matched place is set, and the request method can be limited and it match it by using the key to the ARRAY form.

The regular expression can be used for the key. As a result, it is possible to correspond to a flexible request pattern. Moreover, because a rear reference can be received, it is treatable in the CODE reference.

# 1.
qr{^baz_(.+)}=> { 
   # 2.
   qr{^boo_(.+)}=> sub {
      my($d, $e, $p)= @_;
      },
  },

As for such dispatch, the rear reference obtained by '# 2' is passed to the third argument of the CODE reference. In a word, the value of $p is a rear reference corresponding to the regular expression of '# 2' and the content is ARRAY reference.

To process the rear reference obtained in the place of '# 1', tied is used and extracted from the value of $e->action.

# Because the key to '# 1' is a regular expression that picks up a rear reference,
# piece zero element of $e->dispatch->parts is set with Tie SCALAR.
# When the value is done in tied, and the ARRAY object is acquired, the second
# element is a value of a rear reference.
my $p1_array= tied($e->dispatch->parts->[0])->[2];

# And, the element number of a rear reference wanting it is specified.
my $match= $p1_array->[0];

# By the way, '# 2' can be similarly acquired from the first element.
#  my $p2_array= tied($e->dispatch->parts->[1])->[2];

'_begin' is executed from the one of a shallower hierarchy. When $e->finished is set on the way, '_begin' of a hierarchy that is deeper than it is not processed.

It processes it after the fact when '_end' key is defined. Even if it is executed from the one of the hierarchy with deeper matched action, and $e->finished is set on the way, '_end' processes '_end' of a shallower hierarchy. Therefore, it is necessary to check $e->finished on the code side.

  • mode_param, dispatch_map

WARNING

Some specifications have changed because of Egg::Response-3.12.

The change part is as follows.

  • When a rear reference was obtained, the content of the action of the correspondence element was preserved with Tie Scalar.

    As a result, acquiring a rear reference for a shallower hierarchy became possible.

  • It was made to do with ARRAY when the attribute was set to the key to dispatch.

    Because the referred attribute is few, it is not troublesomely seen to set it with HASH easily.

  • The order of evaluating '_begin' reversed.

    Processing it from a deeper hierarchy before to a shallow hierarchy is still strange.

EXPORT FUNCTION

It is a function exported to the controller and the dispatch class of the project.

refhash ([HASH])

Received HASH is returned and after Tie is done with Tie::RefHash, the content is returned by the HASH reference.

When the key to the ARRAY form is set to 'dispatch_map', it is indispensable.

It doesn't go well even if the reference is passed to this function. Please pass it by a usual HASH form.

# This is not good.
my $hashref = refhash ({
   [qw/ _default 0 /, 'index page.']=> sub {},
   [qw/ help     0 /, 'help page.' ]=> sub {},
   });

# It is OK.
my $hashref = refhash (
   [qw/ _default 0 /, 'index page.']=> sub {},
   [qw/ help     0 /, 'help page.' ]=> sub {},
   );

METHODS

Egg::Dispatch has been succeeded to.

dispatch

The 'Egg::Dispatch::Standard::handler' object is returned.

my $d= $e->dispatch;

HANDLER METHODS

mode_now

The value in which the list of the matched action ties by '/' delimitation is returned.

label ( [NUMBER] )

The list of the matched action is returned by the ARRAY reference.

When the figure is given, the corresponding value is returned.

SEE ALSO

Egg::Release, Egg::Dispatch, Tie::RefHash,

AUTHOR

Masatoshi Mizuno <lushe@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2008 Bee Flag, Corp. <http://egg.bomcity.com/>.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.