Reputation: 2710
How can I call a slim function from another function in different php page
Here 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
});
Upvotes: 3
Views: 7077
Reputation: 2625
Hello I have this in my production app.
Signature of route:
$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();
In my particular instance I need to capture the exact page and send it in an email. So by running the route and capturing the HTML i can simply send an html email with the captured page body. It works flawlessly.
Upvotes: 6
Reputation: 2108
New answer because it's a complete different solution (feel free to downvote the first ;-) ):
If you want to use anonymous functions, you can assign them to a variable and later call by variable.
because they're defined in the global context, they are not available until you give them to the other anonymous functions with use
or global
.
This is how it could be done with anonymus functions:
$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();
This will output execing func2... execing func1... 42
Upvotes: 5
Reputation: 2108
Even if i don't understand why you need to do this, try the following style (alternative in Slim to call functions)
$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);
});
Upvotes: 5