2

I'm following this tutorial to get a deeper understanding in dependency injection.

Because our host is still on PHP5.3, I'm using Aura\Web -components for HTTP response/request. Dependency injection is done with Auryn\Injector

So far I've managed to get project running, but I cannot use the alias as a classname that I've defined in the injector:

$injector = new \Auryn\Injector;

$injector->alias( 'Http\Request', '\Aura\Web\Request' );
$injector->share( '\Aura\Web\Request' );
$injector->define( '\Aura\Web\Request', array(
 ':client'  => new \Aura\Web\Request\Client( $_SERVER ),
 ':content' => new \Aura\Web\Request\Content( $_SERVER ),
 ':globals' => new \Aura\Web\Request\Globals(
  new \Aura\Web\Request\Values( $_COOKIE ),
  new \Aura\Web\Request\Values( $_ENV ),
  new \Aura\Web\Request\Files( $_FILES ),
  new \Aura\Web\Request\Values( $_POST ),
  new \Aura\Web\Request\Values( $_GET ),
  new \Aura\Web\Request\Values( $_SERVER )
 ),
 ':headers' => new \Aura\Web\Request\Headers( $_SERVER ),
 ':method'  => new \Aura\Web\Request\Method( $_SERVER, $_POST ),
 ':params'  => new \Aura\Web\Request\Params,
 ':url'     => new \Aura\Web\Request\Url( $_SERVER )
 )
);

In my controller I would like to use this alias:

namespace Example\Controllers;
use Http\Request;

class Homepage {
    public function __construct( Request $request) { ... }
}

This throws the following error:

Could not make \Example\Controllers\Homepage: Class Http\Request does not exist

I can fix this by declaring the class as:

namespace Example\Controllers;
use Aura\Web\Request;

class Homepage {
   public function __construct( Request $request) { ... }
}

Which probably makes the dependency injection a bit.. useless? Auryn still supplies the proper arguments ($action = $injector->make( $action_class );), but why isn't the alias accepted?

I hope my question makes some sense. :-)

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
PaulH
  • 52
  • 7

1 Answers1

0

This is an Auryn "issue". If you read their docs, you can see that alias works like this:

// Tell the Injector class to inject an instance of V8 any time
// it encounters an Engine type-hint
$injector->alias('Engine', 'V8');

So all it does is it maps a default implementation (class) to an interface.

Right now you are mixing my own HTTP library with the one from Aura which won't work because Aura\Web\Request is not implementing the Http\Request interface from my library (obviously).

So what you need to do is type hint either for an interface or class that is part of the Aura library.

I recommend you give the Auryn docs a read so that you understand how alias and the other commands work.

Patrick
  • 922
  • 11
  • 22