如何在不同的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
});
Run Code Online (Sandbox Code Playgroud)
您好我的生产应用程序中有这个.
路线签名:
$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();
Run Code Online (Sandbox Code Playgroud)
在我的特定实例中,我需要捕获确切的页面并将其发送到电子邮件中.因此,通过运行路线并捕获HTML,我可以简单地发送带有捕获的页面主体的html电子邮件.它完美无瑕.
即使我不明白为什么需要这样做,也请尝试以下样式(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);
});
Run Code Online (Sandbox Code Playgroud)
新的答案,因为它是一个完全不同的解决方案(随意关注第一个;-)):
如果要使用匿名函数,可以将它们分配给变量,然后按变量调用.因为它们是在全局上下文中定义的,所以在用useor或者将它们赋予其他匿名函数之前它们是不可用的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();
Run Code Online (Sandbox Code Playgroud)
这将输出 execing func2... execing func1... 42
| 归档时间: |
|
| 查看次数: |
5561 次 |
| 最近记录: |