【问题标题】:Slim Framework call a slim function from another function in different php pageSlim 框架从不同 php 页面中的另一个函数调用一个 slim 函数
【发布时间】:2015-02-15 21:52:46
【问题描述】:

如何从不同 php 页面中的另一个函数调用一个 slim 函数

这里是我的.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

});

【问题讨论】:

    标签: php slim


    【解决方案1】:

    您好,我的生产应用中有这个。

    路线签名:

    $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 电子邮件。它完美无瑕。

    【讨论】:

      【解决方案2】:

      即使我不明白您为什么需要这样做,请尝试以下样式(在 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);
      
      });
      

      【讨论】:

      • 因为我没有你的功能(在页面开头定义功能)假设我想知道:)
      • 但建议 +1。
      【解决方案3】:

      新答案,因为它是一个完全不同的解决方案(请随意对第一个投反对票 ;-)):

      如果你想使用匿名函数,你可以将它们分配给一个变量,然后通过变量调用。 因为它们是在全局上下文中定义的,所以在您使用 useglobal 将它们提供给其他匿名函数之前,它们不可用。

      这是使用匿名函数的方式:

      $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

      【讨论】:

      • 当在 slim 中没有任何其他选项时很好的答案 :) 但我仍在等待有人说“不,你不能(或更好)”
      • 好的,您正在寻找 Slim 特定的解决方案?可能是redirect()(很好,从未见过这个功能;-))
      • 很好的答案。我用这个在我想在 PUT 之后返回更新的 json 对象并且它是一个构建对象的皮塔,所以我只是通过相应的 get 退出
      猜你喜欢
      • 2015-12-14
      • 1970-01-01
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-21
      相关资源
      最近更新 更多