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 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?