【问题标题】:Get param in Routing - Slim Framework 3在路由中获取参数 - Slim Framework 3
【发布时间】:2016-01-26 20:19:38
【问题描述】:

在这种情况下如何获取参数?

$this->get('/{id}', function($request, $response, $args) {
  return $response->withJson($this->get('singleSelect'));
});

$this->appContainer['singleSelect'] = function ($id) {
  return $this->singleSelect($id);
};

public function singleSelect($id) {
  return $id;
}

提前致谢。

更新

我的解决方案:

$app->group('/id', function () {
    $this->get('/{id}', function($request, $response, $args) {
        $this['container'] = $args; //work with $args inside the container
        return $this->singleSelect($id);
    });
});

【问题讨论】:

    标签: php containers slim slim-3


    【解决方案1】:

    如果我正确理解服务无法访问路由参数。您只能访问容器本身,但从中获取有关参数的信息可能会很棘手(例如$container->getRoutes()['<routename>']->getArguments() 其中<routename> 路由器可能有子路由等)

    恕我直言,您的代码应如下所示:

    $container = new \Slim\Container;
    $app = new \Slim\App($container);
    
    class Example {
        public function singleSelect($id) {
            return $id;
        }
    }
    
    $container['example'] = function () {
        return new Example();
    };
    
    $app->group('/id', function () {
        $this->get('/{id}', function($request, $response, $args) {
            return $response->withJson($this->example->singleSelect($args['id']));
        });
    });
    
    $app->run();
    

    【讨论】:

    • 在我的问题更新中,上面还有另一个解决方案。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 2018-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    相关资源
    最近更新 更多