[RFC] Apache2::DirBasedHandler

[RFC] Apache2::DirBasedHandler

am 20.01.2008 00:52:07 von Adam Prime

I've found myself writing similar bits of code repeatedly, and figure
what i'm doing is probably common enough that something like this
could have a place on CPAN. I'm mostly curious about if the name is
appropriate.

This is just a base class designed to speed the implementation of
handlers that happen to work in this way. I plan to do a subclass of
this that'll have the generic TT generation stuff i'm always using
stuck into it as well. I could also see other generic base classes
like this that might want to make uri's differently.

The idea of this handler is basically to take $r->uri, and map that to
a function. Similar to Apache::Dispatch, similar to what Catalyst
does and similar to about a lot of other code i'm sure. This thing
does work slightly differently though, because i often need it to.

Here's a quick outline of what it does.

Given a request for /foo/bar/haha/hoho

It will loop through the split uri checking for the existance of
subroutines like this:

foo_bar_haha_hoho_page
foo_bar_haha_page
foo_bar_page
foo_page
index

As soon as it finds one, it uses that function to generate the page.
The part of the URI that isn't in the function name gets stuck into an
array reference and passed into the actual page generation function
function like this

$self->function($r,$uri_leftovers,$args)

$args is whatever you want it to be, and is generated by a call to
$self->init($r).

here's some code to look at:

http://kabob.ca/perl/DirBasedHandler.pm

Any input would be appreciated. I don't really even know if anyone
other than me is writing ResponseHandlers in this fashion, or if
everyone is just using Registry, or one of the various frameworks.

Adam

Re: [RFC] Apache2::DirBasedHandler

am 07.02.2008 06:10:30 von Adam Prime

The first version of this is now on CPAN, if anyone is interested in it.

Adam

Quoting adam.prime@utoronto.ca:

>
> I've found myself writing similar bits of code repeatedly, and figure
> what i'm doing is probably common enough that something like this
> could have a place on CPAN. I'm mostly curious about if the name is
> appropriate.
>
> This is just a base class designed to speed the implementation of
> handlers that happen to work in this way. I plan to do a subclass of
> this that'll have the generic TT generation stuff i'm always using
> stuck into it as well. I could also see other generic base classes
> like this that might want to make uri's differently.
>
> The idea of this handler is basically to take $r->uri, and map that to
> a function. Similar to Apache::Dispatch, similar to what Catalyst
> does and similar to about a lot of other code i'm sure. This thing
> does work slightly differently though, because i often need it to.
>
> Here's a quick outline of what it does.
>
> Given a request for /foo/bar/haha/hoho
>
> It will loop through the split uri checking for the existance of
> subroutines like this:
>
> foo_bar_haha_hoho_page
> foo_bar_haha_page
> foo_bar_page
> foo_page
> index
>
> As soon as it finds one, it uses that function to generate the page.
> The part of the URI that isn't in the function name gets stuck into an
> array reference and passed into the actual page generation function
> function like this
>
> $self->function($r,$uri_leftovers,$args)
>
> $args is whatever you want it to be, and is generated by a call to
> $self->init($r).
>
> here's some code to look at:
>
> http://kabob.ca/perl/DirBasedHandler.pm
>
> Any input would be appreciated. I don't really even know if anyone
> other than me is writing ResponseHandlers in this fashion, or if
> everyone is just using Registry, or one of the various frameworks.
>
> Adam
>
>

Re: [RFC] Apache2::DirBasedHandler

am 07.02.2008 11:16:17 von aw

I'm not a guru in the matter either, but it sounds interesting.
It looks like it could give a way to do things REST-like, but simpler.

adam.prime@utoronto.ca wrote:
> The first version of this is now on CPAN, if anyone is interested in it.
>
> Adam
>
> Quoting adam.prime@utoronto.ca:
>
>>
>> I've found myself writing similar bits of code repeatedly, and figure
>> what i'm doing is probably common enough that something like this
>> could have a place on CPAN. I'm mostly curious about if the name is
>> appropriate.
>>
>> This is just a base class designed to speed the implementation of
>> handlers that happen to work in this way. I plan to do a subclass of
>> this that'll have the generic TT generation stuff i'm always using
>> stuck into it as well. I could also see other generic base classes
>> like this that might want to make uri's differently.
>>
>> The idea of this handler is basically to take $r->uri, and map that to
>> a function. Similar to Apache::Dispatch, similar to what Catalyst
>> does and similar to about a lot of other code i'm sure. This thing
>> does work slightly differently though, because i often need it to.
>>
>> Here's a quick outline of what it does.
>>
>> Given a request for /foo/bar/haha/hoho
>>
>> It will loop through the split uri checking for the existance of
>> subroutines like this:
>>
>> foo_bar_haha_hoho_page
>> foo_bar_haha_page
>> foo_bar_page
>> foo_page
>> index
>>
>> As soon as it finds one, it uses that function to generate the page.
>> The part of the URI that isn't in the function name gets stuck into an
>> array reference and passed into the actual page generation function
>> function like this
>>
>> $self->function($r,$uri_leftovers,$args)
>>
>> $args is whatever you want it to be, and is generated by a call to
>> $self->init($r).
>>
>> here's some code to look at:
>>
>> http://kabob.ca/perl/DirBasedHandler.pm
>>
>> Any input would be appreciated. I don't really even know if anyone
>> other than me is writing ResponseHandlers in this fashion, or if
>> everyone is just using Registry, or one of the various frameworks.
>>
>> Adam
>>
>>
>
>
>

Re: [RFC] Apache2::DirBasedHandler

am 10.02.2008 17:39:12 von Adam Prime

I uploaded a .02 release last night which i think will make something
like this even easier to accomplish. This version adds a function
called uri_to_function, which takes a RequestRec (or derived) object,
and an array of uri_bits and returns a function name that should be
used to to handle that request.

I don't really know REST, but from my limited knowledge it seems to me
that it would be good to be able to easily separate code for post,
get, put, and delete to the same URI. So with this new version, if you
subclass uri_to_function to work like this:

return join(qq[_],(lc($r->method),@$uri_bits)) . qq[_page];

then you can very easily achieve that seperation.

I also uploaded Apache2::DirBasedHandler::TT, which is a subclass of
DirBasedHandler that will pass a Template object, and a base set of TT
vars into each function call. It's more or less just in illustration
of how i thought one might use the init function in
Apache2::DirBasedHandler.

Feedback welcome

Adam


Quoting André Warnier :

> I'm not a guru in the matter either, but it sounds interesting.
> It looks like it could give a way to do things REST-like, but simpler.
>
> adam.prime@utoronto.ca wrote:
>> The first version of this is now on CPAN, if anyone is interested in it.
>>
>> Adam
>>