您可以从 CodeIgniter 中的另一个模型内部访问模型吗?

2022-01-02 00:00:00 model authentication php codeigniter

我正在使用需要身份验证的 CodeIgniter 编写 web 应用程序.我创建了一个模型来处理我的所有身份验证.但是,我找不到从另一个模型内部访问此身份验证模型的方法.有没有办法从另一种模式内部访问模型,或者在 CodeIgniter 内部处理身份验证的更好方法?

I am writing a webapp using CodeIgniter that requires authentication. I created a model which handles all my authentication. However, I can't find a way to access this authentication model from inside another model. Is there a way to access a model from inside another mode, or a better way to handle authentication inside CodeIgniter?

推荐答案

通常,您不希望在对象内创建对象.这是一个坏习惯,相反,编写清晰的 API 并将模型注入您的模型中.

In general, you don't want to create objects inside an object. That's a bad habit, instead, write a clear API and inject a model into your model.

<?php
// in your controller
$model1 = new Model1();
$model2 = new Model2();
$model2->setWhatever($model1);
?>

相关文章