我正在尝试将数据库变量传递给中间件函数。但是我收到以下错误:
Fatal error: Call to undefined method Slim\Route::prepare() in /Applications/MAMP/htdocs/website/models/feedback.php on line 11 with the code: "$results = $db->prepare("INSERT INTO ... "
代码如下:
function newsletter(){
$app = \Slim\Slim::getInstance();
$route = $app->request->post('route');
$email = $app->request->post('email_subscribe');
$subscribe = $app->request->post("subscribe");
$oFeedback = new Feedback();
if(!empty($email)) {
$cleanEmail = filter_var($email, FILTER_SANITIZE_EMAIL);
$type="newsletter";
$oFeedback->saveFeedback($db,NULL,$cleanEmail,NULL,$type);
}
}
我在路由中调用中间件函数,如下所示:
$app->post('/triparticle', 'newsletter', function() use($app, $db){
})->name("triparticle_post");
你能帮我吗?
最佳答案
您应该能够使用 anonymus 函数来做到这一点。
您的代码可能如下所示:
$newsletter = function ($db){
return function () use ($db){
$app = \Slim\Slim::getInstance();
$route = $app->request->post('route');
$email = $app->request->post('email_subscribe');
$subscribe = $app->request->post("subscribe");
$oFeedback = new Feedback();
if(!empty($email)) {
$cleanEmail = filter_var($email, FILTER_SANITIZE_EMAIL);
$type="newsletter";
$oFeedback->saveFeedback($db,NULL,$cleanEmail,NULL,$type);
}
};
};
$app->post('/triparticle', $newsletter($db), function(){
})->name("triparticle_post");
这是描述的here .
关于php - 如何将变量传递给中间件函数? [SLIM框架],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34399510/