Please note: This was ZF beta 1. I doubt this is the same for current release
I was just attempting to inject dependencies into some of my classes. I thought I had this down as I was already managing it with quite a few things like injecting database configurations, DB adapters and auth adapters into my UserMapper class.
I was trying to do the same with some other classes of mine and couldn’t work out at all why it just wasn’t working like my UserMapper class was. Now I see that the UserMapper class is being injected into a controller which has an alias set for it at the top of the config file.
So I guess without having it first injected by using a controller… how am I supposed to be injecting stuff into my Models? I’m using the standard ZF2 Skeleton by EvanDotPro
My Models are in:
Application\Model\
Application\Model\DbTable
My config currently looks something like:
<?php
return array(
'bootstrap_class' => 'Application\Bootstrap',
'layout' => 'layouts/layout.phtml',
'di' => array(
'instance' => array(
'alias' => array(
'index' => 'Application\Controller\IndexController',
'view' => 'Zend\View\PhpRenderer'
),
'Zend\View\HelperLoader' => array(
'parameters' => array(
'map' => array(
'url' => 'Application\View\Helper\Url',
),
),
),
'Zend\View\HelperBroker' => array(
'parameters' => array(
'loader' => 'Zend\View\HelperLoader',
),
),
'Application\Controller\IndexController' => array(
'parameters' => array(
'userMapper' => 'Application\Model\UserMapper',
'flashMessenger' => 'Zend\Mvc\Controller\Plugin\FlashMessenger'
)
),
'Application\Model\UserMapper' => array(
'parameters' => array(
'db' => 'Zend\Db\Adapter\PdoMysql',
'authAdapter' => 'Zend\Authentication\Adapter\DbTable',
'userTable' => 'Application\Model\DbTable\UserTable'
)
),
'Application\Model\DbTable\UserTable' => array(
'parameters' => array(
'config' => 'Zend\Db\Adapter\PdoMysql',
)
),
// Auth adapter
'Zend\Authentication\Adapter\DbTable' => array(
'parameters' => array(
'zendDb' => 'Zend\Db\Adapter\PdoMysql',
'tableName' => 'users',
'identityColumn' => 'username',
'credentialColumn' => 'password',
'credentialTreatment' => 'MD5(CONCAT(?,"OSalTyr$"))'
)
),
// DB Adapter
'Zend\Db\Adapter\PdoMysql' => array(
'parameters' => array(
'config' => array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'blahblah',
),
),
),
// View
'Zend\View\PhpRenderer' => array(
'parameters' => array(
'resolver' => 'Zend\View\TemplatePathStack',
'options' => array(
'script_paths' => array(
'application' => __DIR__ . '/../views',
),
),
'broker' => 'Zend\View\HelperBroker',
),
),
),
),
'routes' => array(
'default' => array(
'type' => 'Zend\Mvc\Router\Http\Regex',
'options' => array(
'regex' => '/(?P<controller>[^/]+)(/(?P<action>[^/]+)?)?',
'spec' => '/%controller%/%action%',
'defaults' => array(
'controller' => 'error',
'action' => 'index',
),
),
),
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'index',
'action' => 'index',
),
),
),
),
);
Thanks, Dominic
Ahah… I found out why this wasn’t working.
Although the above config is correct and I’m defining what I want injecting into what… I wasn’t actually using the injected instance of the object in question.
I thought when using ‘new thingy();’ that DI would magically pick up that it has dependencies and apply them when needed but this is not the case…
What I infact needed to do… was inject the
thingyclass into myUserMapperclass like a chain and then use the injected instance instead of creating it anewinstance of it without stuff injected into it.Like:
RegisterControllerrequiresUserMapperso inject it in and use that instanceUserMapperrequiresThingyso inject it in and use that instance