FAQ
So I am trying to serve all static pages out of the root directory.
Should be simple, but I am having trouble. It seems that the regex
matches, but then the handler doesn't execute:

# match anything after the slash that is not a slash
sub files : Regex('/*([^/]*\.(css|js|html))$') {
my ( $self, $c ) = @_;
my ($file, $extension) = @{ $c->req->snippets };
$c->req->path( "$file" );
$c->serve_static;
}


But this is what happens:

[Wed Jun 29 09:08:41 2005] [catalyst] [debug]
**********************************
[Wed Jun 29 09:08:41 2005] [catalyst] [debug] * Request 2 (0.333/s) [26154]
[Wed Jun 29 09:08:41 2005] [catalyst] [debug]
**********************************
[Wed Jun 29 09:08:41 2005] [catalyst] [debug] Requested action is
"booking/style.css" and matched "/*([^/]*\.(css|js|html))$"
[Wed Jun 29 09:08:41 2005] [catalyst] [debug] Snippets are "style.css css"
[Wed Jun 29 09:08:41 2005] [catalyst] [debug] "GET" request for
"booking/style.css" from 192.168.1.3
[Wed Jun 29 09:08:41 2005] [catalyst] [debug] Rendering template
"booking/style.css"
[Template::Service] process(booking/style.css, HASH(0x91be5fc))
[Wed Jun 29 09:08:41 2005] [catalyst] [error] Couldn't render template
"file error - booking/style.css: not found"
[Wed Jun 29 09:08:41 2005] [catalyst] [info] Request took 0.023221s
(43.064/s)

So it is trying to serve the template instead. And if I insert debug
code in the files method it never shows it. What is happening?

Mike

Search Discussions

  • Mike McKay at Jun 29, 2005 at 10:45 am
    Answering my own question:

    Mike McKay wrote:
    So I am trying to serve all static pages out of the root directory.
    Should be simple, but I am having trouble. It seems that the regex
    matches, but then the handler doesn't execute:

    # match anything after the slash that is not a slash
    sub files : Regex('/*([^/]*\.(css|js|html))$') { <snip/>
    code in the files method it never shows it. What is happening?
    From the documentation:
    ---
    If you want to pass variable arguments at the end of a URL, you must
    use regex actions keys with '^' and '$' anchors, and the arguments must
    be separated with forward slashes (/) in the URL
    ---

    So boys and girls, today Mike learned that all RegEx URL handlers need
    to start with a caret.

    Cheers,
    Mike
  • Will Hawes at Jun 29, 2005 at 10:52 am
    I think ideally Catalyst should test for that and throw an error.

    wh

    ----- Original Message -----
    From: Mike McKay <mike@vdomck.org>
    To: The elegant MVC web framework <catalyst@lists.rawmode.org>
    Sent: Wed, 29 Jun 2005 09:48:19 +0100
    Subject: Re: [Catalyst] Help with static and RegEx

    Answering my own question:

    Mike McKay wrote:
    So I am trying to serve all static pages out of the root directory.
    Should be simple, but I am having trouble. It seems that the regex
    matches, but then the handler doesn't execute:

    # match anything after the slash that is not a slash
    sub files : Regex('/*([^/]*\.(css|js|html))$') { <snip/>
    code in the files method it never shows it. What is happening?
    From the documentation:
    ---
    If you want to pass variable arguments at the end of a URL, you must
    use regex actions keys with '^' and '$' anchors, and the arguments must
    be separated with forward slashes (/) in the URL
    ---

    So boys and girls, today Mike learned that all RegEx URL handlers need
    to start with a caret.

    Cheers,
    Mike


    _______________________________________________
    Catalyst mailing list
    Catalyst@lists.rawmode.org
    http://lists.rawmode.org/mailman/listinfo/catalyst
  • Mike McKay at Jun 29, 2005 at 11:35 am

    Will Hawes wrote:
    I think ideally Catalyst should test for that and throw an error.
    That would be great.

    By the way, below is what I finally came up with. Not that I think this
    is best practice, or even the assumed usage for Catalyst::Plugin::Static
    but in case it would be useful to have some more code in the docs. I
    need to to make it ignore case, like m/foo/i - but I don't now how to do
    it with the Regex notation - can anyone help?

    # want to match booking/style.css
    # and style.css
    # match anything after the last slash that is not a slash
    sub staticFiles : Regex('^.*?/*([^/]*\.(css|js|html))$') {
    my ( $self, $c ) = @_;
    my ($file, $extension) = @{ $c->req->snippets };
    $c->req->path( "$file" );
    $c->serve_static;
    }

    sub images : Regex('^.*?/*([^/]*\.(jpg|png|gif|ico))$') {
    my ( $self, $c ) = @_;
    my ($file, $extension) = @{ $c->req->snippets };
    $c->req->path( "img/$file" );
    $c->serve_static;
    }

    Answering my own question:

    Mike McKay wrote:
    So I am trying to serve all static pages out of the root directory.
    Should be simple, but I am having trouble. It seems that the regex
    matches, but then the handler doesn't execute:

    # match anything after the slash that is not a slash
    sub files : Regex('/*([^/]*\.(css|js|html))$') { <snip/>
    code in the files method it never shows it. What is happening?
    From the documentation:
    ---
    If you want to pass variable arguments at the end of a URL, you must
    use regex actions keys with '^' and '$' anchors, and the arguments must
    be separated with forward slashes (/) in the URL
    ---

    So boys and girls, today Mike learned that all RegEx URL handlers need
    to start with a caret.

    Cheers,
    Mike


    _______________________________________________
    Catalyst mailing list
    Catalyst@lists.rawmode.org
    http://lists.rawmode.org/mailman/listinfo/catalyst


    _______________________________________________
    Catalyst mailing list
    Catalyst@lists.rawmode.org
    http://lists.rawmode.org/mailman/listinfo/catalyst
  • David Storrs at Jun 29, 2005 at 4:10 pm

    On Jun 29, 2005, at 5:38 AM, Mike McKay wrote:

    I need to to make it ignore case, like m/foo/i - but I don't now
    how to do it with the Regex notation - can anyone help?
    /foo/i is equivalent to
    /(?i)foo/

    --Dks
  • Mike McKay at Jun 30, 2005 at 7:43 am

    David Storrs wrote:
    /foo/i is equivalent to
    /(?i)foo/
    Excellent. Attached is a patch that would include the following in the
    Catalyst::Plugin::Static documentation.

    # use a Regex to match static content to be served
    # want to match foo/image.JPG and image.jpg
    # ignore case match anything after the last slash (or no slash) that is
    not a slash
    sub images : Regex('(?i)^.*?/*([^/]*\.(jpg|png|gif|ico))$') {
    my ( $self, $c ) = @_;
    my ($file, $extension) = @{ $c->req->snippets };
    $c->req->path( "img/$file" );
    $c->serve_static;
    }

    Is this is right way to submit patches?

    Mike
    -------------- next part --------------
    --- Static.pm 2005-06-30 06:26:37.000000000 +0100
    +++ Static.new 2005-06-30 06:29:14.000000000 +0100
    @@ -23,6 +23,17 @@

    # or specify explicitly if you know better
    $c->serve_static('text/css');
    +
    + # use a Regex to match static content to be served
    + # want to match foo/image.JPG and image.jpg
    + # ignore case match anything after the last slash (or no slash) that is not a slash
    + sub images : Regex('(?i)^.*?/*([^/]*\.(jpg|png|gif|ico))$') {
    + my ( $self, $c ) = @_;
    + my ($file, $extension) = @{ $c->req->snippets };
    + $c->req->path( "img/$file" );
    + $c->serve_static;
    + }
    +

    =head1 DESCRIPTION

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedJun 29, '05 at 10:29a
activeJun 30, '05 at 7:43a
posts6
users3
websitecatalystframework.org
irc#catalyst

People

Translate

site design / logo © 2022 Grokbase