douyou8266 2019-03-01 08:32
浏览 42

Slim Api在Model函数中的dependencies.php中使用$ settings $ logger和$ db in __construct

Good morning

How Can I use $settings $logger and $db in __construct from the $container in dependencies.php in a Model function in a Slim Api?

I have the following setup for my Slim framework Api:

settings.php (has $settings $looger and $db in $container)

/src/Models/DataModel.php

namespace Namespace\Api\Models\v1;

class DataModel
{
    private $settings;
    private $logger;
    private $db;


    function __construct($settings, $logger, $db) {
        $this->settings = $settings;
        $this->logger = $logger;
        $this->db = $db;
    }
    function get() {
         ****
    }
}

routes.php

use Slim\Http\Request;
use Slim\Http\Response;

$app->group('/v1', function () use ($app) {
    $app->GET('/imprint/[{id}]', 'Namespace\Api\Models\v1\DataModel:get');
});

The Error Message I get is:

 Too few arguments to function eRecht24\Api\Models\v1\ImprintModel::__construct()

If I understand correctly I need to instantiate the model like so in order to pass $settings $logger and $db:

$model = new Namespace\Api\Models\v1\DataModel($container->settings, $container->logger, $container->db);

As far as I can tell they ($container->settings etc.) are not resolving however. Is this instantiation correct and where do I put it in order to be able to use $settings $logger and $db in my Model functions?

  • 写回答

2条回答 默认 最新

  • ds3464 2019-03-01 08:59
    关注

    Thanks to fresh morning spirit I figured it out on my own but I really sat on this for 2 hours yesterday... :(

    The DataModel has to look like this:

    class ImprintModel
    {
        protected $c;
        private $settings;
        private $logger;
        private $db;
    
    
        function __construct($container) {
    
            $this->c = $container;
            $this->settings = $container['settings'];
            $this->logger = $container['logger'];
            $this->db = $container['db'];
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL