Skip to content

Instantly share code, notes, and snippets.

/methods.diff Secret

Created March 13, 2015 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4f68e1ed2514e2e6749c to your computer and use it in GitHub Desktop.
Save anonymous/4f68e1ed2514e2e6749c to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojolicious/Guides/Routing.pod b/lib/Mojolicious/Guides/Routing.pod
index f8b1653..34239a7 100644
--- a/lib/Mojolicious/Guides/Routing.pod
+++ b/lib/Mojolicious/Guides/Routing.pod
@@ -706,13 +706,6 @@ Post-processing the response to add or remove headers is a very common use.
Same for pre-processing the request.
- # Allow "_method" query parameter to override request method
- $app->hook(before_dispatch => sub {
- my $c = shift;
- return unless my $method = $c->req->url->query->param('_method');
- $c->req->method($method);
- });
-
# Choose template variant based on request headers
$app->hook(before_dispatch => sub {
my $c = shift;
diff --git a/lib/Mojolicious/Plugin/TagHelpers.pm b/lib/Mojolicious/Plugin/TagHelpers.pm
index bf6ec1b..2e41569 100644
--- a/lib/Mojolicious/Plugin/TagHelpers.pm
+++ b/lib/Mojolicious/Plugin/TagHelpers.pm
@@ -52,18 +52,21 @@ sub _form_for {
my ($c, @url) = (shift, shift);
push @url, shift if ref $_[0] eq 'HASH';
- # POST detection
- my @post;
+ # Method detection
+ my (@post, $method);
if (my $r = $c->app->routes->lookup($url[0])) {
- my %methods = (GET => 1, POST => 1);
+ my %methods;
do {
my @via = @{$r->via || []};
- %methods = map { $_ => 1 } grep { $methods{$_} } @via if @via;
+ %methods = map { $_ => 1 } @via if @via;
} while $r = $r->parent;
- @post = (method => 'POST') if $methods{POST} && !$methods{GET};
+ $method = (sort keys %methods)[0] if !$methods{POST} && !$methods{GET};
+ @post = (method => 'POST') if $method || !$methods{GET};
}
- return _tag('form', action => $c->url_for(@url), @post, @_);
+ my $url = $c->url_for(@url);
+ $url->query({_method => $method}) if $method;
+ return _tag('form', action => $url, @post, @_);
}
sub _hidden_field {
diff --git a/lib/Mojolicious/Routes.pm b/lib/Mojolicious/Routes.pm
index bdabbf2..766d130 100644
--- a/lib/Mojolicious/Routes.pm
+++ b/lib/Mojolicious/Routes.pm
@@ -71,7 +71,7 @@ sub match {
else { $path = $req->url->path->to_route }
# Method (HEAD will be treated as GET)
- my $method = uc $req->method;
+ my $method = uc($req->query_params->param('_method') || $req->method);
$method = 'GET' if $method eq 'HEAD';
# Check cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment