【发布时间】:2020-06-06 16:50:04
【问题描述】:
自从我在 CI 中使用构造函数以来已经有一段时间了。我查看了 CI4 的用户指南,构造函数似乎与 CI3 有点不同。我复制了代码并试用了它,但我收到一条错误消息:Cannot call constructor。
public function __construct(...$params)
{
parent::__construct(...$params);
$model = new ShopModel();
$findAll = [
'shop' => $model->table('shop')->where('brand_name_slug', 'hugo-boss')->findAll()
];
}
从这里,我在网上搜索并看到一个类似的帖子,建议完全删除parent::__construct(...$params); 行。当我这样做时,页面会加载 - 但是当我尝试在 Controller 函数中调用它时 $findAll 数组为 NULL 我需要它:
public function brand_name($brand_name_slug)
{
$model = new ShopModel();
var_dump($findAll['shop']);
$data = [
'shop' => $findAll['shop'],
];
echo view('templates/header', $data);
echo view('shop/view', $data);
echo view('templates/footer', $data);
}
非常感谢您的建议或指点!
【问题讨论】:
-
你需要阅读 PHP 类。
-
所以很明显,我可能会为我正在尝试做的事情向构造函数发出错误的键。我想要做的是将数据库查询
$model->table('shop')->where('brand_name_slug', 'hugo-boss')->findAll()定义为类内其他地方的变量($var),然后在函数控制器内brand_name像这样调用它:data['shop'] = $var我有什么解决方案想做什么? @TimBrownlaw
标签: codeigniter constructor controller construct codeigniter-4