Mojolicious-Plugin-Args
view release on metacpan or search on metacpan
lib/Mojolicious/Plugin/Args.pm view on Meta::CPAN
package Mojolicious::Plugin::Args;
use Mojo::Base 'Mojolicious::Plugin';
use Mojo::JSON 'decode_json';
sub register {
my ( $self, $app, $opts ) = @_;
$opts->{ '-want-detail' } //= 0 unless exists $opts->{ '-want-detail' };
my $want_detail = delete $opts->{ '-want-detail' };
$app->helper( args => sub {
my $c = shift;
my $stash = $c->stash;
my @param = $c->param;
my ( %args, %save );
$args{ $_ } = $c->param( $_ ) for @param;
$save{ $_ } = $stash->{args}{ $_ } for grep { exists $stash->{args} and defined $stash->{args}{ $_ } } keys %args;
my $type = $c->req->headers->header( 'Content-Type' );
if ( ( $c->req->method ne 'GET' and $type and $type =~ 'application/json' ) or
( $c->req->method eq 'GET' and defined $stash->{format} and $stash->{format} eq 'json' and defined $args{json} ) ) {
my @args = keys %args;
my $json = decode_json( $c->req->method eq 'GET' ? delete $args{json} : $c->req->body );
my @json = keys %{ $json };
do {
$args{__args}->{ $_ } = $args{ $_ } for @args; # save to __priv
$args{__json}->{ $_ } = $json->{ $_ } for @json; # for specific access
} if $want_detail;
$args{ $_ } = $json->{ $_ } for @json;
}
%args = ( %args, %save ); # this allows interception and override from route conditions that want to validate/modify and/or hooks
$stash->{args} = \%args;
return wantarray ? %{ $stash->{args} } : $stash->{args};
} ) unless $app->renderer->helpers->{args};
}
# ABSTRACT: gives you back the request parameters as a simple %args hash, even if it's posted in json.
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Mojolicious::Plugin::Args - gives you back the request parameters as a simple %args hash, even if it's posted in json.
=head1 VERSION
version 0.05
=head1 SYNOPSIS
Route something like this:
package App;
use Mojo::Base 'Mojolicious';
sub startup {
my $self = shift;
$self->plugin( 'Mojolicious::Plugin::Args' );
my $r = $self->routes;
$r->any( '/example/test' )->to( 'example#test' );
}
Here's the controller:
package App::Example;
use Mojo::Base 'Mojolicious::Controller';
sub test {
my $self = shift;
my %args = $self->args;
$self->log->debug( 'args', $self->dumper( \%args ) );
$self->render( json => \%args );
}
Now send a POST to it (jQuery example):
$.ajax( {
type: 'POST'
,url: '/example/test'
,contentType: 'application/json'
,dataType: 'json'
,data: JSON.stringify( { foo: 'bar' } )
( run in 1.517 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )