GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Jifty::Dispatcher(3) User Contributed Perl Documentation Jifty::Dispatcher(3)

Jifty::Dispatcher - The Jifty Dispatcher

In MyApp::Dispatcher:

    package MyApp::Dispatcher;
    use Jifty::Dispatcher -base;

    under ['blog', 'wiki'] => [
        run {
            default model => "MyApp::Model::\u$1"
        },
        on PUT 'entries/*' => run {
            set entry_id => $1;
            show '/display/entry';
        },
        on '*/*' => run {
            my ($page, $op) = ($1, $2);
            my $item = get('model')->load($page) or next_rule;

            set item => $item;
            set page => $page;
            set op   => $op;

            show "/display/$op";
        },
        on '*' => run { dispatch "$1/view" },
        on ''  => show '/display/list',
    ];
    under qr{logs/(\d+)} => [
        when { $1 > 100 } => show '/error',
        set model => 'MyApp::Model::Log',
        run { dispatch "/wiki/LogPage-$1" },
    ];
    # ... more rules ...

"Jifty::Dispatcher" takes requests for pages, walks through a dispatch table, possibly running code or transforming the request before finally handing off control to the templating system to display the page the user requested or whatever else the system has decided to display instead.

Generally, this is not the place to be performing model and user specific access control checks or updating your database based on what the user has sent in. You want to do that in your model classes. (Well, we want you to do that, but you're free to ignore our advice).

The Dispatcher runs rules in several stages:

before
before rules are run before Jifty evaluates actions. They're the perfect place to enable or disable Jifty::Actions using "allow" in Jifty::API and "deny" in Jifty::API or to completely disallow user access to private component templates such as the _elements directory in a default Jifty application. They're also the right way to enable Jifty::LetMe actions.

You can entirely stop processing with the "redirect", "tangent" and "abort" directives, though "after" rules will still run.

on
on rules are run after Jifty evaluates actions, so they have full access to the results actions users have performed. They're the right place to set up view-specific objects or load up values for your templates.

Dispatcher directives are evaluated in order until we get to either a "show", "redirect", "tangent" or "abort".

after
after rules let you clean up after rendering your page. Delete your cache files, write your transaction logs, whatever.

At this point, it's too late to "show", "redirect", "tangent" or "abort" page display.

"Jifty::Dispatcher" is intended to replace all the autohandler, dhandler and "index.html" boilerplate code commonly found in Mason applications, but there's nothing stopping you from using those features in your application when they're more convenient.

Each directive's code block runs in its own scope, but all share a common $Dispatcher object.

By default, Jifty::Plugin dispatcher rules are added in the order they are specified in the application's configuration file; that is, after all the plugin dispatchers have run in order, then the application's dispatcher runs. It is possible to specify rules which should be reordered with respect to this rule, however. This is done by using a variant on the "before" and "after" syntax:

    before plugin NAME =>
        RULE(S);
    
    after plugin NAME =>
        RULE(S);

    after app,
        RULE(S)

"NAME" may either be a string, which must match the plugin name exactly, or a regular expression, which is matched against the plugin name. The rule will be placed at the first boundary that it matches -- that is, given a "before plugin qr/^Jifty::Plugin::Auth::/" and both a "Jifty::Plugin::Auth::Basic" and a "Jifty::Plugin::Auth::Complex", the rules will be placed before the first.

"after app" inserts the following "RULES" after the application's dispatcher rules, and is identical to, but hopefully clearer than, "after plugin Jifty => RULES".

"RULES" may either be a single "before", "on", "under", or "after" rule to change the ordering of, or an array reference of rules to reorder.

The current Jifty::Request object.

The current dispatcher object.

Return the argument value.

Match against the current requested path. If matched, set the current context to the directory and process the rule.

The $rule may be an array reference of more rules, a code reference, a method name of your dispatcher class, or a fully qualified subroutine name.

All wildcards in the $match string becomes capturing regex patterns. You can also pass in an array reference of matches, or a regex pattern.

The $match string may be qualified with a HTTP method name or protocol, such as

GET
POST
PUT
OPTIONS
DELETE
HEAD
HTTPS
HTTP

Like "under", except it has to match the whole path instead of just the prefix. Does not set current directory context for its rules.

Just like "on", except it runs before actions are evaluated.

Just like "on", except it runs after the page is rendered.

Like "under", except using an user-supplied test condition. You can stick any Perl you want inside the {...}; it's just an anonymous subroutine.

Run a block of code unconditionally; all rules are allowed inside a "run" block, as well as user code. You can think of the {...} as an anonymous subroutine.

Run a block of code unconditionally, which should return a coderef that is a PSGI streamy response.

Adds an argument to what we're passing to our template, overriding any value the user sent or we've already set.

Adds an argument to what we're passing to our template, but only if it is not defined currently.

Deletes an argument we were passing to our template.

Display the presentation component. If not specified, use the request path as the default page.

Dispatch again using $path as the request path, preserving args.

Break out from the current "run" block and go on the next rule.

Break out from the current "run" block and stop running rules in this stage.

Abort the request; this skips straight to the cleanup stage.

If $code is specified, it's used as the HTTP status code.

Redirect to another URI.

Take a continuation here, and tangent to another URI.

See "Plugins and rule ordering", above.

Jifty::Dispatcher is an Exporter, that is, part of its role is to blast a bunch of symbols into another package. In this case, that other package is the dispatcher for your application.

You never call import directly. Just:

    use Jifty::Dispatcher -base;

in "MyApp::Dispatcher"

Returns an array of all the rules for the stage STAGE.

Valid values for STAGE are

SETUP
RUN
CLEANUP

Creates a new Jifty::Dispatcher object. You probably don't ever want to do this. (Jifty.pm does it for you)

Actually do what your dispatcher does. For now, the right thing to do is to put the following two lines first:

    require MyApp::Dispatcher;
    MyApp::Dispatcher->handle_request;

Handles the all rules in the stage named "NAME". Additionally, any other arguments passed after the stage "NAME" are added to the end of the rules for that stage.

This is the unit which calling "last_rule" skips to the end of.

When handed an arrayref or array of rules (RULESET), walks through the rules in order, executing as it goes.

When handed a single rule in the form of a coderef, "_handle_rule", calls "_do_run" on that rule and returns the result. When handed a rule that turns out to be an array of subrules, recursively calls itself and evaluates the subrules in order.

This method is called by the dispatcher internally. You shouldn't need to.

This method is called by the dispatcher internally. You shouldn't need to.

This method is called by the dispatcher internally. You shouldn't need to.

This method is called by the dispatcher internally. You shouldn't need to.

This method is called by the dispatcher internally. You shouldn't need to.

Returns true if the code block has run once already in this request. This can be useful for 'after' rules to ensure that they only run once, even if there is a sub-dispatch which would cause it to run more than once. The idiom is:

    after '/some/path/*' => run {
        return if already_run;
        # ...
    };

This method is called by the dispatcher internally. You shouldn't need to.

Redirect the user to the URL provided in the mandatory PATH argument.

This method is called by the dispatcher internally. You shouldn't need to.

Take a tangent to the URL provided in the mandatory PATH argument. (See Jifty::Manual::Continuation for more about tangents.)

The method is called by the dispatcher internally. You shouldn't need to.

Take a coderef that returns a PSGI streamy response code.

This method is called by the dispatcher internally. You shouldn't need to.

Don't display any page. just stop.

This method is called by the dispatcher internally. You shouldn't need to.

Render a template. If the scalar argument "PATH" is given, render that component. Otherwise, just render whatever we were going to anyway.

First, this routine runs all the "before" dispatcher rules, then it runs Jifty->web->handle_request(), then it runs all the main "on" rules, evaluating each one in turn. If it gets through all the rules without running an "abort", "redirect" or "show" directive, it "shows" the template originally requested.

Once it's done with that, it runs all the cleanup rules defined with "after".

Returns the regular expression matched if the current request fits the condition defined by CONDITION.

"CONDITION" can be a regular expression, a "simple string" with shell wildcard characters ("*", "?", "#", "[]", "{}") to match against, or an arrayref or hashref of those. It should even be nestable.

Arrayref conditions represents alternatives: the match succeeds as soon as the first match is found.

Hashref conditions are conjunctions: each non-empty hash key triggers a separate "_match_$keyname" call on the dispatcher object. For example, a "method" key would call "_match_method" with its value to be matched against. After each subcondition is tried (in lexicographical order) and succeeded, the value associated with the '' key is matched again as the condition.

Takes an HTTP method. Returns true if the current request came in with that method.

Returns true if the current request is under SSL.

Returns true if the current request is not under SSL.

Takes a condition defined as a simple string and return it as a regex condition.

Private function.

Turns a metaexpression containing "*", "?" and "#" into a capturing regex pattern.

Also supports the non-capturing "[]" and "{}" notations.

The rules are:

  • A "*" between two "/" characters, or between a "/" and end of string, should match one or more non-slash characters:

        /foo/*/bar
        /foo/*/
        /foo/*
        /*
        
  • All other "*" can match zero or more non-slash characters:

        /*bar
        /foo*bar
        *
        
  • Two stars ("**") can match zero or more characters, including slash:

        /**/bar
        /foo/**
        **
        
  • Consecutive "?" marks are captured together:

        /foo???bar      # One capture for ???
        /foo??*         # Two captures, one for ?? and one for *
        
  • The "#" character captures one or more digit characters.
  • Brackets such as "[a-z]" denote character classes; they are not captured.
  • Braces such as "{xxx,yyy}]" denote alternations; they are not captured.

Imports rules from "plugins" in Jifty into the main dispatcher's space.
2014-12-09 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.