Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

*_POST and *_GET; some doc changes

  • Loading branch information...
commit 67b044a8b340be3b6f4ceed99ec32e450b7715a1 1 parent 85a9c83
Zbigniew Lukasiak authored
1  Changes
View
@@ -18,6 +18,7 @@ Revision history for WebNano
docs improvements (Plack::Middleware::Auth::Form mentioned)
{{$NEXT}}
+ there are incompatible changes!
got rid of DirController (add sub search_subcontrollers { 1 } to get the functionality
with standard Controller
path attribute for the Controller class - removing all the path parameter passing
22 lib/WebNano.pm
View
@@ -119,11 +119,11 @@ of HTTP request paths into method calls as in the following examples:
'/page' -> 'MyApp::Controller->page_action()'
'/Some/Very/long/pa/th' -> 'MyApp::Controller::Some::Very->long_action( 'pa', 'th' )
-The first type of dispatching is always available - to get actions
+The dispatching to methods inside a controller is always available - to get actions
dispatched to controllers in subdirs you need to override the C<search_subcontrollers>
-method and make it return a true value.
-Your root controllers should usually have C<sub search_subcontrollers { 1 }>.
-Other controllers also can do that - but only if they
+method and make it return a true value: C<sub search_subcontrollers { 1 }>.
+Usually in your root controller should do that.
+Other controllers also can do it as well - but only if they
do not do their own dispatching to sub-controllers. If a controller has custom
dispatching then you should leave the default C<search_subcontrollers> to avoid
intruducing possible security risks from the automatic dispatching which could
@@ -146,10 +146,10 @@ More advanced dispatching can be done by overriding the C<local_dispatch> method
the Controller class:
around 'local_dispatch' => sub {
- my( $orig, $self, @path) = @_;
- my( $id, $method, @args ) = @path;
+ my( $orig, $self ) = @_;
+ my( $id, $method, @args ) = @{ $self->path };
$method ||= 'view';
- if( $id && $id =~ /^\d+$/ && $self->is_record_method( $method ) ){
+ if( $id && $id =~ /^\d+$/ && $self->record_methods->{ $method } ){
my $rs = $self->app->schema->resultset( 'Dvd' );
my $record = $rs->find( $id );
if( ! $record ) {
@@ -160,7 +160,7 @@ the Controller class:
}
return $self->$method( $record, @args );
}
- return $self->$orig( @path );
+ return $self->$orig();
};
This example checks if the first part of the path is a number - if it is it uses
@@ -171,16 +171,14 @@ Note the need to check if the called method is an allowed one.
If the first part of the url is not a number - then the request is dispatched in
the normal way.
+More examples you can find in the C<examples> subdir.
+
The primary design goal here is to provide basic functionality that should cover most
use cases and offer a easy way to override and extend it for special cases.
In general it is easy to write your own dispatcher that work for your limited use
case - and here you just need to do that, you can override the dispatching only for a
particular controller and you don't need to warry about the general cases.
-The example in F<extensions/WebNano-Controller-DSL/> shows how to create a DSL
-for dispatching (ala Dancer):
-
- get '/some_address' => sub { 'This is some_address in web_dispatch table' };
=head2 Controller object live in the request scope (new controller per request)
7 lib/WebNano/Controller.pm
View
@@ -40,8 +40,11 @@ sub local_dispatch {
$action = $self->can( $name ) if grep { $_ eq $name } @$map;
}
}
- my $method = $name . '_action';
- $action = $self->can( $method ) if !$action;
+ $action = $self->can( $name . '_action' ) if !$action;
+ if( ! $action ){
+ my $method = uc( $self->env->{REQUEST_METHOD} );
+ $action = $self->can( $name . '_' . $method ) if $method eq 'GET' || $method eq 'POST';
+ }
my $out;
if( $action ){
$out = $action->( $self, @parts );
5 t/10.main.t
View
@@ -19,6 +19,11 @@ test_psgi(
$res = $cb->(GET "/mapped url");
like( $res->content, qr/This is the mapped url page/ );
+ $res = $cb->(GET "/postonly");
+ is( $res->code, 404 , 'GET for a POST controller' );
+ $res = $cb->(POST "/postonly");
+ like( $res->content, qr/This is a method with _POST postfix/ );
+
$res = $cb->(GET "NestedController/some_method");
like( $res->content, qr/This is a method with _action postfix/ );
$res = $cb->(GET "NestedController/safe_method");
2  t/lib/MyApp/Controller.pm
View
@@ -36,5 +36,7 @@ sub streaming_action {
}
}
+sub postonly_POST { 'This is a method with _POST postfix' }
+
1;
Please sign in to comment.
Something went wrong with that request. Please try again.