explain dispatch subroutine more
[catagits/Web-Simple.git] / lib / Web / Simple.pm
index 5307bbb..fe1722e 100644 (file)
@@ -36,6 +36,9 @@ sub _export_into {
     *{"${app_package}::redispatch_to"} = sub {
       $app_package->_construct_redispatch($_[0]);
     };
+    *{"${app_package}::subdispatch"} = sub ($) {
+      $app_package->_construct_subdispatch($_[0]);
+    };
     *{"${app_package}::default_config"} = sub {
       $app_package->_setup_default_config(@_);
     };
@@ -149,6 +152,8 @@ It also exports the following subroutines:
 
   redispatch_to '/somewhere';
 
+  subdispatch sub (...) { ... }
+
 and creates a $self global variable in your application package, so you can
 use $self in dispatch subs without violating strict (Web::Simple::Application
 arranges for dispatch subroutines to have the correct $self in scope when
@@ -203,8 +208,14 @@ the subroutines passed to it, which then creates your Web::Simple
 application's dispatcher from these subs. The prototype of the subroutine
 is expected to be a Web::Simple dispatch specification (see
 L</DISPATCH SPECIFICATIONS> below for more details), and the body of the
-subroutine is the code to execute if the specification matches. See
-L</DISPATCH STRATEGY> below for details on how the Web::Simple dispatch
+subroutine is the code to execute if the specification matches.
+
+Each dispatcher is given the dispatcher constructed from the next element
+of the arrayref as its next dispatcher, except for the final element, which
+is given the return value of NameOfApplication->_build_final_dispatcher
+as its next dispatcher (by default this returns a 500 error response).
+
+See L</DISPATCH STRATEGY> below for details on how the Web::Simple dispatch
 system uses the return values of these subroutines to determine how to
 continue, alter or abort dispatch.
 
@@ -251,6 +262,26 @@ Thus if you receive a POST to '/some/url' and return a redipstch to
 '/other/url', the dispatch behaviour will be exactly as if the same POST
 request had been made to '/other/url' instead.
 
+=head2 subdispatch
+
+  subdispatch sub (/user/*/) {
+    my $u = $self->user($_[1]);
+    [
+      sub (GET) { $u },
+      sub (DELETE) { $u->delete },
+    ]
+  }
+
+The subdispatch subroutine is designed for use in dispatcher construction.
+
+It creates a dispatcher which, if it matches, treats its return value not
+as a final value but an arrayref of dispatch specifications such as could
+be passed to the dispatch subroutine itself. These are turned into a dispatcher
+which is then invoked. Any changes the match makes to the request are in
+scope for this inner dispatcher only - so if the initial match is a
+destructive one like .html the full path will be restored if the
+subdispatch fails.
+
 =head1 DISPATCH STRATEGY
 
 =head2 Description of the dispatcher object