Laravel 到控制器的路由被调用两次

ste*_*son 3 php wamp laravel laravel-artisan

我仍在学习 laravel,并使用 v5.4.28 创建了一个项目,也使用 dev v5.5 进行了测试,两个版本都调用控制器两次,从而插入 2 条记录。仅当我使用 WAMP 并访问http://localhost/laravel/public/test?test=123 时才会发生这种情况

如果我使用 php artisan 服务并访问此 http://127.0.0.1:8000/test?test=123 它只插入一次

如果我使用 chrome 检查并查看我的网络选项卡,我会看到该页面在 wamp 上被调用两次。

这是正常的吗?

将我的routes/web.php编辑为

Route::get('/', function () {
return view('home');
});
Route::get('/test', 'testController@store');
Run Code Online (Sandbox Code Playgroud)

并创建了一个 testController

class testController extends Controller
{
    public function store()
    {
        $test = new Test;
        $test ->user_id = request('test');
        $test ->save();

        //if i put a redirect here then it wont insert twice
    }
}
Run Code Online (Sandbox Code Playgroud)

Mur*_*rwa 9

如果您有一个中间件对next. 例如,假设您有一个父中间件的句柄函数:

public function handle($request, Closure $next, ...$guards){
    // Your logic here
    return $next($request);
}
Run Code Online (Sandbox Code Playgroud)

现在,假设您有一个子中间件,如下所示:

public function handle($request, Closure $next, ...$guards){
    // Note this call to the parent's next
    $resp = parent::handler($request, $next, $guards);
    // Extra logic here
    // ... and now we make another call to next
    return $next($request);
}
Run Code Online (Sandbox Code Playgroud)

next为了避免这种情况,请确保对中间件的句柄函数进行一次调用。

https://laracasts.com/discuss/channels/laravel/controller-methods- Called-twice-causing-duplicate-database-entries