如何在不同的php页面中调用另一个函数的slim函数
这里My.php:
$app->get('/list/:id',function($id)
{
//fill array here
echo $somearray;
});
$app->post('/update/:id',function($id)
{
//do update operation here
//!Important : How can do this?
echo $app->get('My.php/list/$id'); // call function above
});
答案 0 :(得分:6)
您好我的生产应用程序中有这个。
路线签名:
$app->get('xxx/:jobid', function ($jobid) use($app) {})->name('audit_edit');
//Get The Route you want...
$route = $app->router()->getNamedRoute("audit_edit"); //returns Route
$route->setParams(array("jobid" => $audit->omc_id)); //Set the params
//Start a output buffer
ob_start();
$page2 = $route->dispatch(); //run the route
//Get the output
$page = ob_get_clean();
在我的特定实例中,我需要捕获确切的页面并通过电子邮件发送。因此,通过运行路线并捕获HTML,我可以简单地发送带有捕获的页面主体的html电子邮件。它完美无缺。
答案 1 :(得分:5)
新的答案,因为它是一个完全不同的解决方案(随意关注第一个;-)):
如果要使用匿名函数,可以将它们分配给变量,然后按变量调用。
因为它们是在全局上下文中定义的,所以在您使用use
或global
将其提供给其他匿名函数之前,它们将无法使用。
这是匿名函数的完成方式:
$app->get('/list/:id', ($list=function($id){
//fill array here
echo "executing func1... ";
return 42;
}));
$app->get('/update/:id',function($id) use (&$list){
echo "executing func2... ";
echo $list(42);
});
$app->run();
这将输出execing func2... execing func1... 42
答案 2 :(得分:4)
即使我不明白为什么你需要这样做,尝试以下样式(Slim中的替代方法来调用函数)
$app->get('/list/:id', 'listById');
$app->post('/update/:id','updateById');
function listById($id)
{
//fill array here
echo $somearray;
});
function updateById($id){
//do update operation here
echo listById($id);
});