Another I-did-not-sleep-enough question, I\'m sure. I\'m posting it as a sacrifice to the Elder God Murphy: as soon as I expose my moronity for all to see, I\'m guarante
ensure POST URLs do not end with a slash. -- but Laravel 4.1 users check the update below, first.
=======
When it works, it's not superstition - it's science :-(
as soon as I expose my moronity for all to see, I'm guaranteed to find by myself that answer that would otherwise elude me for hours
I will argue that Laravel4's HTML message could have been just a bit more informative.
The grep
found as expected the origin of the redirect:
grep -r "Redirecting to" *
vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/RedirectResponse.php: <title>Redirecting to %1$s</title>
vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/RedirectResponse.php: Redirecting to <a href="%1$s">%1$s</a>.
at that point, a simple backtrace found the origin, very early in Laravel4 spin-up:
bootstrap/start.php:
...
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/
$app = new Illuminate\Foundation\Application;
$app->redirectIfTrailingSlash();
As soon as I saw the redirectIfTrailingSlash
, I realized that the two URLs sent by the form and by jQuery were not the same:
... action="/search/?uid=1701"> <--- TRAILING SLASH AFTER 'search'
... url: '/search', <--- NO TRAILING SLASH
data: {
uid : 1701,
query: $('#searchterm').val()
},
...
Why this should happen I don't quite grok, but the solution is fiendishly simple:
remove the slash from the POST action field.
(and ensure .htaccess
has no rule to consider the URL a "directory" and add back the slash - but if it had, jQuery would have failed too).
Apparently, the matter got reviewed in Laravel 4.1. The upgrade mentions
Removing Redirect Trailing Slash
In your bootstrap/start.php file, remove the call to $app->redirectIfTrailingSlash(). This method is no longer needed as this functionality is now handled by the .htaccess file included with the framework.
Next, replace your Apache .htaccess file with this new one that handles trailing slashes.