Where to load controller dependency classes?

I am creating my own primitive MVC framework with PHP, and I wonder where should I load / create instances of the corresponding controller dependencies?

In the constructor of each controller (tightly connected) or injected (loosely coupled)?

The only part of the latter, which I'm not too sure about, is that to create dependencies at the boot level, outside the MVC paradigm, before introducing. Not every controller uses the same dependencies as the parent. I would have to create them all, which would also create a lot of overhead.

I saw that some existing frameworks do this as $this->load->model('model'); // CodeIgniterin the constructor, but I don’t know why they do it.

+4
source share
5 answers

I would advise you to add dependencies, so your controllers are less attached to your structure. This will simplify the transition to another structure.

About instantiating dependencies: I suggest you use (or implement) a container of dependency injection . This container must contain factories that can create services.

In an ideal situation, your controllers are also services (which means that they also have factories in the dependency injection container).

, , .
, ( ) . , .

Pimple .

PS: CodeIgniter . , .

+3

: / ?

. "/" "/".

, , ( ), . , , ? ..

, . , , . Dependency Injection, . . .

- . , , , registry. .

. . , , 500 , , .

.

, , , .

, . use . use , . . , , .

, : Setter Injection Reference Injection . , . .

: $this->load->model('model'); // CodeIgniter?

CodeIgniter - . , . $this->load - . "" ( , ) .

Loader CodeIgniters , , , . - . , . , $this->load->model('model'); , model . , model. , ( ): https://github.com/EllisLab/CodeIgniter/blob/develop/system/core/Loader.php#L223.

+1

Symfony, Symfony. , , Symfony, , .

, :

  • , .

BaseController, , . Silex: http://silex.sensiolabs.org/ a micro Framework

, .

0

" " conatiner ( )?

<?php

class SomeController
{
  public function __construct($container)
  {
    $this->service1 = $contanier->get('service1);
  }
  //...
}

, , , , ServiceLocator, - .

, , :

class SomeController
{
  public function __construct($service1)
  {
    $this->service1 = $service1;
  }
  //...
}

:

// this uses Pimple notation, I hope you get the point
$container['controller'] = function($c) {
  return SomeController($c['service1']);
}

, , , , :

1) - ,

<?php

class ProxyService
{
  /**
   * @var Service1Type
   */
  private $actualService;

  public function __construct()
  {
    $this->actualService = null;
  }

  private function initialize()
  {
    $this->actualService = new Service1(); // This operation may take some time thus we deferred as long as possible
  }

  private function isInitialized()
  {
    return $this->actualService === null;
  }

  public function someActionOnThisService()
  {
    if (!$this->isInitalized()) {
      $this->initalize();
    }

    $this->actualService->someActionOnThisService();
  }

- . -,

2)

, .

Paul M. Jones ( Aura Framework) MVC-Refinement, IMHO , .

, , - (, , : , ).

0

, __autoload(), . :

function __autoload($className) {
    require "/path/to/the/class/file/$className.php";
}

, . if-else switch , .

__autoload() , PHP , new, class_exists(), call_user_method() .., /. __autoload() - , PHP .

spl_autoload_register() __autoload() .

: http://php.net/manual/en/function.autoload.php http://php.net/manual/en/function.spl-autoload-register.php

-3

All Articles