Injecting cache to a module - dependency-injection

I am trying to inject cache to a module using module.config.php with no luck. My module.config.php looks like this
return array(
'di' => array(
'instance' => array(
'AssetLoader\Module' => array(
'parameters' => array(
'cache' => 'Zend\Cache\Storage\Adapter\Filesystem',
),
),
),
),
);
and in my Module.php
public function setCache(\Zend\Cache\Storage\Adapter\Filesystem $cache)
{
die(__FUNCTION__);
$this->cache = $cache;
}
But nothing happens. I expect script to die but for some reason this function is never executed. I am sure I am doing something wrong but can someone explain how to inject a cache (or any other object in that matter) to the module?

This question is answered on Zend mailing list now.
See here
Matthew pointed out that Module classes weren't pulled out from locator, so you can't use DI to inject resources to the modules. See the link to see how it's done.

Related

Zf2 ScnSocialAuth HybridAuth zfcuser and Routes

Using ZF2 to customise an Entity based on ZfcUser. Trying to use ScnSocialAuth and got a bit of a problem.
The problem is that I am using custom routes ('/account' instead of '/user') and when implementing ScnSocialAuth I cannot get the social code into my custom zfcuser view...?
I have \\view\zfc-user\user\register.php which overrides the zfcuser registration.
I have a customised route:
'account' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/account',
),
),
These are my zfc config modification within \my-module\config\module.config.php
'zfcuser' => array(
// telling ZfcUser to use our own class
'user_entity_class' => 'WMember\Entity\WMember',
// telling ZfcUserDoctrineORM to skip the entities it defines
'enable_default_entities' => false,
'table_name' => 'w_member',
'login_redirect_route' => 'account',
),
My global \config\application.config.php
'ScnSocialAuth',
'MyModule1',
'ZfcBase',
'ZfcUser',
'BjyAuthorize',
'GoalioMailService',
'GoalioForgotPassword',
'my-user-module',
Therefore, after all this:
I can see my own extended User registration form by navigating to
/account/register with no Social login links visible
I can see the ScnSocialAuth when navigating to /user/register
a) I cannot create the view in my module to override \vendor\scn-social-auth\user\register.phtml as was done with zfcuser
Please help with getting ScnSocialAuth to work with my custom route setup.
If this is just wrong please let me know as I'm not ZF2 expert. Happy to take 'constructive' criticism.
Saw these posts: How to (correctly) extend ScnSocialAuth\Authentication\Adapter\HybridAuth::authenticate() method?
and this as a result of the above post:
https://github.com/SocalNick/ScnSocialAuth/issues/202
NOTE: still running ZF-2.3* due to PHP 5.3,5.4
Instead of adding a custom route to your config, you need to over-ride the zfcuser route
<?php
// #file MyUserModule/config/module.config.php
return array(
// other config ...
'router' => array(
'routes' => array(
'zfcuser' => array(
'options' => array(
// this is the only change needed to route zfcuser to /account
'route' => '/account',
),
),
),
),
// more config ...
);
The ScnSocialAuth module uses the forward() plugin to render the content from zfcusers register view (and login view iirc), which means it will only ever look at the zfcuser route and completely ignore your custom route. The only way to have it use your custom route would be to replace ScnSocialAuths UserController with your own using identical code but forwarding to your custom route (much more work there, and still the potential to break anything else that expects zfcuser to be the route used)

How to utilize old classes with the underlines in Zend Framework 2

I need to autoload old style classes in ZF2. The classes are located in vendor directory.
What is the best way to this?
ZF's StandardAutoloader class provides a fallback autoloading mechanism for non-namespaced classes. Try to set fallback_autoloader key to true in your Application module's getAutoloaderConfig() method something like below:
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src',
'Ancientlib' => '/path/to/ancient/library'
),
'fallback_autoloader' => true,
)
);
}
Also there is an article by Rob Allen about this subject.
Provides more details about this subject: Using Zend\Loader\Autoloader

ServiceManager returns same Instance while shared = false

I defined an alias for MyClass that I want to be retrievable via ServiceManager, see module.config.php example below. When I retrieve an instance of MyClass in my controller via $this->serviceLocator->get('MyClass') I get a new instane the first time, and then the same instance. Do I miss something in my configuration or should I file a bug?
module.config.php
return array(
'di' => array(
'instance' => array(
'alias' => array(
'MyClass' => 'Some\Namespace\MyClass',
),
'MyClass' => array(
'shared' => false,
),
),
),
);
As my edit was not accepted, I will answer this question myself. In the end I moved to ServiceManager thanks to Stoyan Dimov who pushed me into the right direction. Unfortunately his answer was not correct. I had to use invokables instead of alias. Here is the correct example:
return array(
'invokables' => array(
'MyClass' => 'Some\Namespace\MyClass',
),
'shared' => array(
'MyClass' => false,
),
);
When I was using alias instead of invokables I got an exception that no service with that name can be is available, which means that aliasdoes not work, there has to be an existing service defined by invokables, factories, services or abstract_factories.
Following the previous example I could use:
...
'alias' => array(
'SomethingDifferent' => 'MyClass',
),
...
With a call to $this->serviceLocator->get('SomethingDifferent')I would receive an instance of Some\Namespace\MyClass.
I don't know about the DI but the ServiceManager expects your to specify shared (false) in the root of the configuration array for the manager. Something like this:
'invokables' => array( // Note that you can use also 'factories', 'abstract_factories', etc.
'Some\Namespace\MyClass' => 'Some\Namespace\MyClass',
),
'alias' => array(
'MyClass' => 'Some\Namespace\MyClass',
),
'shared' => array(
'MyClass' => false,
),
See the ServiceManager example configuration on the manual
Hope this helps :)
Stoyan

How to put translator in Zend2 form

Iam new guy to Zend framework and currently Iam working on Zend2...I want to ask about Translator usage in Zend forms....If i want to use translator i directly using for labels in form view i.e.form_view.php like
$this->formLabel()->setTranslator($translator, 'date_of_birth');
But I want to add the translator at the form only i.e.in src/my_module/Form/UserForm.php
like
$this->add(array(
'name' => 'date_of_birth',
'attributes' => array(
'type' => 'text',
'id' => 'date_of_birth',
),
'options' => array(
'label' => 'DateOfBirth',
), //Here there is any option to put translator
));
Please help me...any answer would be help for me like I asked
Thanks in advance
You don't really need to do that. Since the the Translator that is set up using the factory-key translator will automatically be injected into the Form.
The best approach (in my opinion) is to make extensive use of the translator text_domain:
'translator' => array(
'locale' => 'de_DE',
'translation_file_patterns' => array(
array(
'type' => 'phparray',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.php',
'text_domain' => 'MyModuleTextDomain'
),
),
),
With this setup, the Files of your Module will automatically be inserted into the default TranslatorService which every Zend\Form knows of.
So ultimately all you have to do is make the ViewHelpers know of the TextDomain that you are using. And this is done in the following manner:
$this->formLabel()->setTranslatorTextDomain('MyModuleTextDomain');
$this->formButton()->setTranslatorTextDomain('MyModuleTextDomain');
$this->formElementErrors()->setTranslatorTextDomain('MyModuleTextDomain');
You need to do this once inside your respective view.phtml before(!) using the ViewHelpers like $this->formElement($element) or $this->formCollection($form)
And that's really all there is to it. I recall having seen a discussion somewhere about making it easier to pass along Text-Domain-Data, but i can't find it right now. So things may get a little easier in the future ;) For now, 3 lines are all that's needed though!
above answer is quite unnecessary ... as your translator was added automatically to zend form for rendering form labels and ....
only use this code in your module config :
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'phparray',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.php',
),
),
),
if u use the correct view helpers for rendering form elements (or whole form) it will automatically translated
This is not a recommended approach because forms are translated automatically if you have a translator configured (which you do if you are using the Skeleton Application). However, since you asked how to use the translator directly within your form, I will show you how you can do it. Please carefully consider if you really want to do this, as I cannot imagine a use case where it would be necessary.
To do exactly what you were asking, you can inject the translator into your form. You can do this either in your controller or in a factory. I will be using a factory in this example because it is more DRY.
// In your module's config file
'service_manager' => array(
'factories' => array(
'YourModule\Form\YourForm' => function($sm) {
$translator = $sm->get('Translator');
return new \YourModule\Form\YourForm($translator);
},
),
),
Then in your form class, you can do like this:
namespace YourModule\Form;
class RegisterForm extends \Zend\Form\Form {
public function __construct($translator) {
// Do something
$translated_string = $translator->translate('string to translate');
}
}
Then in your controller, you can do like this:
$your_form = $this->servicelocator->get('YourModule\Form\YourForm');
Or if you don't want to use the factory, you can choose to not add it and do like this instead:
$your_form = new \YourModule\Form\YourForm($this->servicelocator->get('Translator'));
I would recommend going with the factory, though.

Zend Framework 2 Zend_DB MasterSlaveFeature

The documentation mentions the ability to do master-slave by passing the MasterSlaveFeature to a TableGateway constructor along with the slave adapter as a parameter to the MasterSlaveFeature. My question is how to get access to that slave adapter when instantiating the TableGateway object.
I'm setting my master up in the main global and local configuration files, as per the quick start. How would I go about configuring the slave adapter(s) and how would I set up more than one slave?
Is there a working example of how to do this?
UPDATE: I've made some progress on this. In the global config file, I added another array key called slave and entered the connection information there. Then I created a SlaveAdapterServiceFactory class which mimics the behavior of Zend\Db\Adapter\AdapterServiceFactory but grabs the "slave" config key values instead of "db". Then, in my module config where I do the constructor injection of the master adapter, I also grab the slave adapter and pass it to my model table as the constructor parameter to a new MasterSlaveFeature instance. I also had to alter my model table class to look for Feature stuff getting passed in the constructor. Since the modal table class extends AbstractTableGateway I would expect it to know what to do with the Feature. Hopefully that assumption is correct. This all sounds really confusing! Here is some code. First the global.php from the config/autoload directory:
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=zf2test;host=one.host.com',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'slave' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=zf2test;host=two.host.com',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter'
=> 'Zend\Db\Adapter\AdapterServiceFactory',
'My\Db\Adapter\SlaveAdapter'
=> 'My\Db\Adapter\SlaveAdapterServiceFactory'
),
)
);
Here is the code from the module's module.config.php file:
return array(
'service_manager' => array(
'factories' => array(
'Album\Model\AlbumTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$slaveAdapter = $sm->get('My\Db\Adapter\SlaveAdapter');
$table = new AlbumTable($dbAdapter, new MasterSlaveFeature($slaveAdapter));
return $table;
}
)
)
);
I haven't had a chance to test if the queries are actually going to the correct connections but I'm not getting any errors.
One other question is how to automatically pass these adapters to any models that get created instead of having to do this for each and every model that you instantiate.
This should help you out: MasterSlaveFeature How-To
However talking with the current developer on this area of ZF2 there does appear to be a bug. I've filed a report based on his recommendation so once that is resolved, the example I have shown should work, combined with what you have as well =)

Resources