我正在使用苗条3与laravel雄辩“照亮/数据库”:“^ 5.6”,
在我的苗条代码示例中
在Container中定义语言类
$container['language'] = function($container){
return new \App\Library\Language($container);
};
在语言类中我有函数getLanguageId()并且此值在start应用程序中传递(在此示例中返回语言id = 1)
在模型博客中
<?php
use Illuminate\Database\Eloquent\Model;
class Blog extends Model
{
protected $table = 'blog';
protected $primaryKey = 'blog_id';
public function getLanguageId(){
// I need access to Slim container here
// Example return $container->get('language')->getLanguageId()
// It's possible to do ?
}
}
谢谢
1 回答
通过构造函数传递语言ID:
不要依赖于任何地方的容器而落入服务定位器反模式 . 所以创建这样的对象: