I have update zf2 on the latest version and i receive this error:
http://jsfiddle.net/8Ft6d/
Some mandatory parameter was added for translation?
This is my translator config:
'translator' => array(
'locale' => 'it_IT',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'service_manager' => array(
'aliases' => array(
'translator' => 'MvcTranslator',
),
),
and this is what i call inside the Module.php::onBootstrap()
$translator = $serviceManager->get('translator’);
Thanks
What is happening here is that most probably, the DiAbstractServiceFactory is kicking in before an abstract factory that is responsible of fetching the MvcTransator instance.
You will likely have to switch the order in which abstract factories are being used, or remove your 'di' config from your modules or autoload config, since its presence will automatically cause addition of the DiAbstractServiceFactory to the ServiceManager.
I've had the same issue myself this morning after upgrading from 2.2.6 to 2.3.0.
There's a bug in ZF2.3.0 which causes the Di module to fail when trying to create an instance of the MvcTranslator (see: https://github.com/zendframework/zf2/pull/5959, where #Ocramius and noopable came up with the solution).
Until the fix is rolled out to the framework, you'll need to change the following code in Zend\ServiceManager\Di\DiAbstractServiceFactory from:
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return $this->instanceManager->hasSharedInstance($requestedName)
|| $this->instanceManager->hasAlias($requestedName)
|| $this->instanceManager->hasConfig($requestedName)
|| $this->instanceManager->hasTypePreferences($requestedName)
|| $this->definitions->hasClass($requestedName);
}
to:
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
if ($this->instanceManager->hasSharedInstance($requestedName)
|| $this->instanceManager->hasAlias($requestedName)
|| $this->instanceManager->hasConfig($requestedName)
|| $this->instanceManager->hasTypePreferences($requestedName)
) {
return true;
}
if (! $this->definitions->hasClass($requestedName) || interface_exists($requestedName)) {
return false;
}
return true;
}
Related
I'm trying to register a bunch of custom DBAL types. When I run the migrations:diff I get the exception:
Fatal error: Class 'App\Persistence\Models\Types\Money' not found in D:\development\projects\project\vendor\doctrine\dbal\lib\Doctrine\DBAL\Types\Type.php on line 174
I've tried to do this either by registering it after all Doctrine settings and using a event subscriber:
class DoctrineCustomTypesEventSubscriber implements Subscriber {
public function getSubscribedEvents() {
return [Events::postConnect];
}
public function postConnect(ConnectionEventArgs $args) {
Type::addType('money', "App\Persistence\Models\Types\Money");
Type::addType('geopoint', "App\Persistence\Models\Types\Point");
Type::addType('geoarea', "App\Persistence\Models\Types\Area");
}
}
$doctrineCustomTypesSubscriber = new App\Persistence\DoctrineCustomTypesEventSubscriber();
$app['db.event_manager']->addEventSubscriber($doctrineCustomTypesSubscriber);
$app->register(new Dflydev\Provider\DoctrineOrm\DoctrineOrmServiceProvider, array(
'orm.proxies_dir' => $app['APP_ROOT_DIR'].'/app/persistence/proxies',
'orm.em.options' => array(
'mappings' => array(
array(
'type' => 'annotation',
'namespace' => 'App\Persistence\Models',
'path' => $app['APP_ROOT_DIR'].'/app/persistence/models',
'use_simple_annotation_reader' => false,
),
),
),
));
Update
Placing the registration before all orm settings doesn't work either:
use Doctrine\DBAL\Types\Type;
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'db.options' => array('url' => $app['APP_DB_CONN_URL']),
));
Type::addType('money', "App\Persistence\Models\Types\Money");
Type::addType('geopoint', "App\Persistence\Models\Types\Point");
Type::addType('geoarea', "App\Persistence\Models\Types\Area");
What am I doing wrong here?
Also can you tell me where do I put these registrations:
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping(...)?
The type classes should exist and be registered in autoloader, so that they can be instantiated by FQCN associated with type.
I localize product, and i'm newb in PHP,ZF2. I have .PO/.MO files with english and japanese translation. How to configure ZF2 to use these .mo translation in all library\Zend\Validator\* files instead of default messages e.g
protected $messageTemplates = array(
self::NOT_GREATER => "The input is not greater than '%min%'",
self::NOT_GREATER_INCLUSIVE => "The input is not greater or equal than '%min%'"
);
I've read these link but it doesnt help me.
I'm have custom Translator which extends MvcTranslator and config looks like:
```
),
'service_manager' => array(
'aliases' => array(
'translator' => 'MvcTranslator',
),
'factories' => array(
// ...
'MvcTranslator' => function ($services) {
$config = $services->get('config');
$config = isset($config['translator']) ? $config['translator'] : array();
$translator = \Application\I18n\Translator::factory($config);
$translator->setEscaper(new \Application\Escaper\Escaper);
// add event listener for context fallback on missing translations
$translator->enableEventManager();
$translator->getEventManager()->attach(
$translator::EVENT_MISSING_TRANSLATION,
array($translator, 'handleMissingTranslation')
);
// establish default locale settings
$translator->setLocale($translator->getLocale() ?: 'en_US');
$translator->setFallbackLocale($translator->getFallbackLocale() ?: 'en_US');
// try to guess locale from browser language header (using intl if available)
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])
&& (!isset($config['detect_locale']) || $config['detect_locale'] !== false)
) {
$locale = extension_loaded('intl')
? \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE'])
: str_replace('-', '_', current(explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE'])));
$translator->setLocale(strlen($locale) ? $locale : $translator->getLocale());
}
return $translator;
},
),
),
'translator' => array(
'locale' => 'en_US',
'detect_locale' => true,
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo'
),
),
),
```
I thought i should put Zend\Validator\AbstractValidator::setDefaultTranslator($translator);
somewhere there..but nothing changed..Should i write some code in validators or what ?
I am working through the zend framework 2 tutorial application. I have set up my Album Module directory as follows:
I am running into an error when I start my MAMP server, Fatal error: Uncaught exception 'Zend\ModuleManager\Exception\RuntimeException' with message 'Module (Album) could not be initialized.'
If I comment out the Album module from the following code (in /config/application.config.php):
'modules' => array(
'Application',
'Album',
),
I get to the skeleton application homepage.
Here is my /module/Album/Module.php code:
namespace Album;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Album\Model\Album;
use Album\Model\AlbumTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
class Module implements AutoloaderProviderInterface, ConfigProviderInterface {
public function getAutoloaderConfig() {
return array(
’Zend\Loader\ClassMapAutoloader’ => array(
__DIR__ . ’/autoload_classmap.php’,
),
’Zend\Loader\StandardAutoloader’ => array( ’namespaces’ => array(
__NAMESPACE__ => __DIR__ . ’/src/’ . __NAMESPACE__,
),
),
);
}
public function getConfig() {
return include __DIR__ . ’/config/module.config.php’;
}
public function getServiceConfig() {
return array(
’factories’ => array(
’Album\Model\AlbumTable’ => function($sm) {
$tableGateway = $sm->get(’AlbumTableGateway’);
$table = new AlbumTable($tableGateway);
return $table;
},
’AlbumTableGateway’ => function ($sm) {
$dbAdapter = $sm->get(’Zend\Db\Adapter\Adapter’);
$resultSetPrototype = new ResultSet(); $resultSetPrototype->setArrayObjectPrototype(new Album());
return new TableGateway(’album’, $dbAdapter, null, $resultSetPrototype);
},
),
);
}
}
And here is my module.config.php code in /module/Album/config/:
return array(
’controllers’ => array(
’invokables’ => array(
’Album\Controller\Album’ => ’Album\Controller\AlbumController’,
),
),
’view_manager’ => array(
’template_path_stack’ => array(
’album’ => __DIR__ . ’/../view’,
),
),
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),'
);
I have read through a few people who have ran in to similar situations, but their issues were due to misspelled/incorrectly named classes. I don't see anything wrong with my code (even went as far as copying/pasting directly from tutorial).
Can someone give me some suggestions?
Thanks
I had the same problem, and the solution was to start every .php file with <?php
This is not clear in the tutorial (if you just copy the code from there), and it was the reason I was getting the same exception.
The only reason i could see is that you should replace all ’ with normal single quotes '.
using ’ can results in unexpected behaviors.
There is an error in the tutorials module.config.php file.
Change 'id' => '[0-9]+',
to 'id' => '[0-9]*',
+ means one or more digits. If you just call http://zf2-tutorial.localhost/album/ there is no digit in the url so the rewriterule doesn't match. Change from + to * (0 or more)
I had the exact same issue, my issue and solution were
the very first command in the tutorial is:
php composer.phar create-project --stability="dev" zendframework/skeleton-application path/to/install
It was cut off my screen so I copy/pasted the command to the command line without reading the end. So my app directory structure included a directory ./path/to/install where all of the installation files were, including application.config.php.
Moving everything in /path/to/install/ to the root directory of the app allowed Zend to find the Album module.
Note: /path/to/install/module contains the album module but will fail if you do
mv ./path/to/install/* .
so be sure to move the Application module at /path/to/install/module into app_root_dir/module/.
You can check composer.json weather include the following:
"autoload": {
"psr-4": {
"Application\\": "module/Application/src/",
"Album\\": "module/Album/src"
}
},
Please check your Module::getConfig() ,
and play with :
return include __DIR__ . '/../config/module.config.php';
I have added '../' prefix and it works well .
EDIT: Yup, is bug.
I suspect this is a bug, so I've submitted it as an issue at https://github.com/zendframework/zf2/issues/6051, but just in case it's just me being stupid it doesn't hurt to ask here as well. :)
After upgrading ZF2 from 2.2.6 to 2.3.0 I'm receiving the following series of uncaught exceptions inside Zend\Di\Di:
Zend\Di\Exception\RuntimeException: Invalid instantiator of type "NULL" for "Zend\I18n\Translator\TranslatorInterface". in /path/to/vendor/zendframework/zendframework/library/Zend/Di/Di.php on line 305
Zend\ServiceManager\Exception\ServiceNotCreatedException: An exception was raised while creating "Zend\I18n\Translator\TranslatorInterface"; no instance returned in /path/to/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php on line 909
Zend\ServiceManager\Exception\ServiceNotCreatedException: An abstract factory could not create an instance of zendi18ntranslatortranslatorinterface(alias: Zend\I18n\Translator\TranslatorInterface). in /path/to/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php on line 1070
Zend\ServiceManager\Exception\ServiceNotCreatedException: An exception was raised while creating "MvcTranslator"; no instance returned in /path/to/vendor/zendframework/zendframework/library/Zend/ServiceManager/ServiceManager.php on line 909
Unfortunately, I can't work out why exactly this is happening, but the I18n module worked fine prior to the upgrade. I have the i18n extension installed and loaded correctly.
I have this in module/Application/config/module.config.php
'service_manager' => [
'aliases' => [
'translator' => 'MvcTranslator',
],
],
and this in each module's module.config.php
'translator' => [
'translation_file_patterns' => [
[
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
],
],
],
The only DI configuration I have so far is this:
'di' => [
'instance' => [
'Zend\View\HelperLoader' => [
'parameters' => [
'map' => [
'zfcUserIdentity' => 'ZfcUser\View\Helper\ZfcUserIdentity',
'zfcUserLoginWidget' => 'ZfcUser\View\Helper\ZfcUserLoginWidget',
],
],
],
],
],
Does 2.3.0 add a requirement to add additional configuration to the DI block in order for I18n to work properly? This isn't reflected in the documentation and I haven't been able to work it out from reading the code so far, but from the exceptions that are being thrown it looks like it's actually trying to create an instance of Zend\I18n\Translator\TranslatorInterface itself rather than Zend\I18n\Translator\Translator as it did previously?
Anyone got any ideas?
Answer found on https://github.com/zendframework/zf2/pull/5959:
In DiAbstractServiceFactory, the following function needs to be changed from:
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return $this->instanceManager->hasSharedInstance($requestedName)
|| $this->instanceManager->hasAlias($requestedName)
|| $this->instanceManager->hasConfig($requestedName)
|| $this->instanceManager->hasTypePreferences($requestedName)
|| $this->definitions->hasClass($requestedName);
}
to:
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
if ($this->instanceManager->hasSharedInstance($requestedName)
|| $this->instanceManager->hasAlias($requestedName)
|| $this->instanceManager->hasConfig($requestedName)
|| $this->instanceManager->hasTypePreferences($requestedName)
) {
return true;
}
if (! $this->definitions->hasClass($requestedName) || interface_exists($requestedName)) {
return false;
}
return true;
}
I believe this has been / is being pull-requested, so it should be included in the next update to ZF2.3.
First ZF2 application, getting there, but I think still missing a think or two when it comes to dependency injection and the ServiceManager.
I have a particular problem at the moment with a new database gateway class I'm writing. I won't to inject a database adapter, so I've implemented AdapterAwareInterface. But the setDbAdapter method is never called in my class. I'm wondering if someone would be so kind as to look at my code and suggest what might be going wrong (or what I'm missing!).
So, here is the class in which I implement AdapterAwareInterface.
<?php
namespace Foo\Database;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Adapter\AdapterAwareInterface;
use Zend\Log\LoggerAwareInterface;
use Zend\Log\LoggerInterface;
class Gateway implements AdapterAwareInterface, LoggerAwareInterface
{
protected $logger = NULL;
protected $db = NULL;
public function setDbAdapter(Adapter $adapter)
{
$this->db = $adapter;
}
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
This is an extract from my module file showing how I configure my service manager:
public function getServiceConfig()
{
return array(
'factories' => array(
....
),
'invokables' => array(
'FooDatabaseGateway' => 'Foo\Database\Gateway',
),
'abstract_factories' => array(
'AbstractFeedParserFactory' => 'Bookmakers\Odds\Feeds\AbstractFeedParserFactory',
),
);
}
This is how I'm testing:
gateway = $this->getServiceLocator()->get('FooDatabaseGateway');
And this is part of my global config:
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=kickoff_manager;host=localhost',
'username' => '****',
'password' => '****',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter'
=> 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);
Many thanks for any help you can provide.
:wq
OK a fresh pair of eyes on this problem this morning. I think this is the write answer.. At least that is to say its working for me. If anyone wants to suggest an entirely different of better approach, then please do so :-).
So the bit is was missing was to use an initializer in my service manager config to call the setDbAdapter function on any class instances that implement AdapterAwareInterface. So in the array I return from getServiceConfig in my Module.php file, I have added the following entry:
public function getServiceConfig() {
return array(
'initializers' => array(
'db' => function($service, $sm)
{
if ($service instanceof AdapterAwareInterface)
{
$service->setDbAdapter($sm->get('Zend\Db\Adapter\Adapter'));
}
}....
I think what I'm missing while learning ZF2 is that there are a lot of building blocks to work with, but you've got to put a lot of them together yourself.
Things are looking good and I'm enjoying the Framework, but there is a lot to learn, and I'm still not convinced by using Server Manager injection rather than good old constructor injection!
:wq