more dispatch strategy documentation
[catagits/Web-Simple.git] / lib / Web / Simple.pm
index 2b2a049..f3bf5a5 100644 (file)
@@ -213,6 +213,144 @@ It creates and returns a response filter object to the dispatcher,
 encapsulating the block passed to it as the filter routine to call. See
 L</DISPATCH STRATEGY> below for how a response filter affects dispatch.
 
-1;
+=head1 DISPATCH STRATEGY
+
+=head2 Description of the dispatcher object
+
+Web::Simple::Dispatcher objects have three components:
+
+=over 4
+
+=item * match - an optional test if this dispatcher matches the request
+
+=item * call - a routine to call if this dispatcher matches (or has no match)
+
+=item * next - the next dispatcher to call
+
+=back
+
+When a dispatcher is invoked, it checks its match routine against the
+request environment. The match routine may provide alterations to the
+request as a result of matching, and/or arguments for the call routine.
+
+If no match routine has been provided then Web::Simple treats this as
+a success, and supplies the request environment to the call routine as
+an argument.
+
+Given a successful match, the call routine is now invoked in list context
+with any arguments given to the original dispatch, plus any arguments
+provided by the match result.
+
+If this routine returns (), Web::Simple treats this identically to a failure
+to match.
+
+If this routine returns a Web::Simple::Dispatcher, the environment changes
+are merged into the environment and the new dispatcher's next pointer is
+set to our next pointer.
+
+If this routine returns anything else, that is treated as the end of dispatch
+and the value is returned.
+
+On a failed match, Web::Simple invokes the next dispatcher with the same
+arguments and request environment passed to the current one. On a successful
+match that returned a new dispatcher, Web::Simple invokes the new dispatcher
+with the same arguments but the modified request environment.
+
+=head2 How Web::Simple builds dispatcher objects for you
+
+In the case of the Web::Simple L</dispatch> export the match is constructed
+from the subroutine prototype - i.e.
+
+  sub (<match specification>) {
+    <call code>
+  }
+
+and the 'next' pointer is populated with the next element of the array,
+expect for the last element, which is given a next that will throw a 500
+error if none of your dispatchers match. If you want to provide something
+else as a default, a routine with no match specification always matches, so -
+
+  sub () {
+    [ 404, [ 'Content-type', 'text/plain' ], [ 'Error: Not Found' ] ]
+  }
+
+will produce a 404 result instead of a 500 by default. You can also override
+the L<Web::Simple::Application/_build_final_dispatcher> method in your app.
+
+Note that the code in the subroutine is executed as a -method- on your
+application object, so if your match specification provides arguments you
+should unpack them like so:
+
+  sub (<match specification>) {
+    my ($self, @args) = @_;
+    ...
+  }
+
+=head2 Web::Simple match specifications
+
+=head3 Method matches
+
+  sub (GET ...) {
+
+A match specification beginning with a capital letter matches HTTP requests
+with that request method.
+
+=head3 Path matches
+
+  sub (/login) {
+
+A match specification beginning with a / is a path match. In the simplest
+case it matches a specific path. To match a path with a wildcard part, you
+can do:
+
+  sub (/user/*) {
+    $self->handle_user($_[1])
+
+This will match /user/<anything> where <anything> does not include a literal
+/ character. The matched part becomes part of the match arguments. You can
+also match more than one part:
+
+  sub (/user/*/*) {
+    my ($self, $user_1, $user_2) = @_;
+
+  sub (/domain/*/user/*) {
+    my ($self, $domain, $user) = @_;
+
+and so on. To match an arbitrary number of parts, use -
+
+  sub (/page/**) {
+
+This will result in an element per /-separated part so matched. Note that
+you can do
+
+  sub (/page/**/edit) {
+
+to match an arbitrary number of parts up to but not including some final
+part.
+
+=head3 Extension matches
+
+  sub (.html) {
+
+will match and strip .html from the path (assuming the subroutine itself
+returns something, of course). This is normally used for rendering - e.g.
+
+  sub (.html) {
+    filter_response { $self->render_html($_[1]) }
+  }
+
+=head3 Combining matches
+
+Matches may be combined with the + character - e.g.
+
+  sub (GET+/user/*) {
+
+Note that for legibility you are permitted to use whitespace -
+
+  sub(GET + /user/*) {
+
+but it will be ignored.
+
+=cut
 
 1;