Pass arguments in Slim DI service

試著忘記壹切 提交于 2019-12-12 18:35:48

问题


I have a service that I want to access from a route but pass arguments to.

$container = new \Slim\Container();    
$container['myService'] = function($arg1, $arg2) {
        //my code here
};
$app = new \Slim\App($container);

and inside my route, I try to call the service like so:

$this->myService('my arg1', 'my arg2');

This is not working. When I try to call it without specifying the arguments, it works.

How to call with arguments? Or is that an alternative way to specify a function or method to be called from inside a route?


回答1:


so you are pretty close.

$container = new \Slim\Container();    
$container['myService'] = function ($c) { 
    return function($arg1, $arg2) {
        //my code here
    }
};
$app = new \Slim\App($container);

$app->get('/', function ($req, $res, $args) {
    $this->myService($a, $b);
});

This should work.

Optionally, with your original code... you have to save it to a variable first before invoking it.

$service = $this->myService;
$service('my arg1', 'my arg2');

Both of these should work.



来源:https://stackoverflow.com/questions/36993560/pass-arguments-in-slim-di-service

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!