[dancer-users] Automating loading of index.html

Mark Wood-Patrick mwoodpatrick at gmail.com
Sun Apr 28 13:33:49 BST 2013


That seemed to break regular URL's like

	/wishlist/mark

But this does appear to work

hook 'before' => sub {
        var note => 'Boo';
        my $url = request->path_info();
        $url =~ s/\/$//;
        $url .= "/index.html";
        my $path = "$FindBin::RealBin/public$url";

        if ( -r $path ) {
            return redirect $url
        }
    };

BTW are most folks using Dancer 2 or Dancer 1?

Mark

-----Original Message-----
From: Jashank Jeremy [mailto:jashank at rulingia.com] 
Sent: Saturday, April 27, 2013 11:01 PM
To: Mark Wood-Patrick
Cc: Perl Dancer users mailing list
Subject: Re: [dancer-users] Automating loading of index.html

G'day,

On Sat, Apr 27, 2013 at 10:32:20PM -0700, Mark Wood-Patrick wrote:
>I need to display index.html for any subdirectory where that file 
>exists not just the root

I suspect you probably want to look at the way that `auto_page` is
implemented, although you may need to appropriate and modify it: in D1, it's
render_autopage and _autopage_response in `Dancer::Renderer`, and in D2,
it's in `Dancer2::Handler::AutoPage`.

I can't really think of any other ways to do it off-hand, other than with a
catch-all route, but that would fall over, I suspect, if you needed a route
at `/todomvc/architecture-examples/yui` as well as at
`/todomvc/architecture-examples/yui/index.html`.  A route something like

    get qr{(.*)} => sub {
        my ($path) = splat;
        return redirect $path."/index.html"
            if -e settings('views_dir').$path."/index.html";
        return send_error(404);
    };

-- assuming a setting, `views_dir`, pointing at your views directory;
alternately, `setting('appdir')."/views"` should work, given a fairly
standard setup -- should work.

Jashank

--
Jashank Jeremy
WWW	rulingia.com/~jashank
PGP	D05D79F1 41DA2FB5 233E0565 ACC5E467 25A5C309



More information about the dancer-users mailing list