Laravel-4 routes with parameters gets called twice

狂风中的少年 提交于 2019-12-07 15:22:31

问题


I noticed strange behaviour occuring for my activation route for an application that I'm developing using laravel and after hours of debugging through xdebug etc, it came into an observation that my route was actually called twice at times. To recreate the situation, I created a fresh new laravel project with default files and just added a simple route as a test that does the following:

Route::get('/activate/{code}', function($code)
{
   file_put_contents('/home/fa/testproj/route_called.txt', $code . "\n",   FILE_APPEND);
});

Basically what this simple route does is to take the route parameter passed on the URI and logs it into a file. Strangely, I noticed that the routes were called twice for code parameter that I typed on the URI (such as http://myserver/activate/123456789), but only once for repeated codes (probably due to some session caching?). The content of the 'route_called.txt' file for multiple tries was as follow:

123456789
123456789
123838384242
123838384242
123838384242        <---- notice that it's called only once for repeated parameter
1238383842424657347
1238383842424657347
332211576347
332211576347
332211576347    <---- notice that it's called only once for repeated parameter
1238376540897326
1238376540897326

The laravel files are fresh and I've not changed anything except for the four lines of route codes as above. I have standard files provided by laravel in the public folder:

-rw-rw-r-- 1 fa fa    0 Apr 15 16:06 favicon.ico
-rw-rw-r-- 1 fa fa  356 Apr 15 16:06 .htaccess
-rw-rw-r-- 1 fa fa 1586 Apr 15 16:06 index.php
drwxrwxr-x 2 fa fa 4096 Apr 15 16:06 packages/
-rw-rw-r-- 1 fa fa   24 Apr 15 16:06 robots.txt

The .htaccess is untouched too as provided by laravel:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

My server is Ubuntu 12.04LTS, running Apache 2.2.22 with all the standard php modules required by laravel.

Some people say elsewhere that this could be due to missing favicon.ico issue but laravel has taken care of this problem by already providing us with the favicon.ico file as above.

Any help is much appreciated. Many thanks!


回答1:


Its ok folks, I managed to find the answer why the strange behaviour was so. It's nothing to do with Laravel at all. It seems that the internet filtering device at the ISP level was what sending the duplicate HTTP requests to my server. I guess the device would send duplicate requests mimicking our browsing page requests in order to check whether the sites that we visit complies with the filtering policies... Had to redesign some logics of my app in order to cater situations like this.




回答2:


I had same problem and i solved it. My problem was from laravel middlewares. Please check your custom middlewares. Hope help : ).. .




回答3:


I called it at the first line of my custom middleware:

$response = $next($request);

and used this variable to next line where i need to use the closure: $next($request)

i removed the first line and now it worked!




回答4:


Hey I was having the same issue in the AWS hosted app, non in local...after hours trying figure it out what was happening, we found that for some reason(maybe something in the deployment process) a Unit Test of this route was triggering the call twice.....if this is your case try commenting any Unit Test and then find why that behavior is happening!!!!



来源:https://stackoverflow.com/questions/23422703/laravel-4-routes-with-parameters-gets-called-twice

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!