I am using http://ift.tt/1O0qBbe as my Dependency Injection Container. I am making project in MVC and I am trying to not break MVC pattern.
I am loading Database, Json, etc. classes from DI Container (defined before routing, etc.), but I need models for every controller. Is this right way how to load Models with their own dependencies inside Controller?
Controller:
abstract class Controller
{
protected $di;
protected $db;
protected $latte;
protected $json;
//etc...
public function __construct(League\Container\Container $container, array $args)
{
$this->di = $container;
$this->db = $container->get('database');
$this->json = $container->get('json');
//etc...
$this->args = $args;
$this->setup();
}
}
class ApiController extends Controller
{
function setup()
{
$this->model = new ApiModel($this->di);
}
public function getGames()
{
//..
}
}
Model:
class Model
{
protected $db;
protected $json;
public function __construct(League\Container\Container $container)
{
$this->json = $container->get('json');
$this->db = $container->get('database');
}
}
class ApiModel extends Model
{
/**
* Get games
* @return mixed
*/
public function getGames()
{
//db queries etc...
}
}
Something saying me, that this is not right way.
Aucun commentaire:
Enregistrer un commentaire