【问题标题】:php slim passing arguments for named functionsphp slim为命名函数传递参数
【发布时间】:2019-04-21 21:54:28
【问题描述】:

我正在使用 PHP slim 框架版本 2。在他们的文档中,他们没有指定如何将参数传递给调用 如何在函数中接收参数。

$app->get('/books/{id}', 'getBook')

function getBook($id){
var_dump($id);
    die('there');

}

但它给了我一个错误,要么找不到要么

All Route middleware must be callable

【问题讨论】:

    标签: php rest slim


    【解决方案1】:

    @owaishanif786 我不知道这种方式 ($app->get('/books/{id}', 'getBook')) 你在你的问题中使用的,但如果我想调用一个函数 slim framework 2 那么我就按照下面的方式做,请检查一下:

    $app->get('/total', function() use($app){
            test(2);
        });
    
        function test($id){
            echo "yes you were right $id";
        }
    

    现在只需调用 /total api 你的函数 test() 与参数 $id 将调用

    【讨论】:

      【解决方案2】:

      截至Slim v3

      $app->get('/books/{id}', function ($request, $response, $args) {
          // $args['id']
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-30
        • 1970-01-01
        • 2019-11-23
        • 2011-05-11
        • 2018-07-06
        • 2012-10-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多