Autoloading Zend Framework 2 packages with the StandardAutoloader - zend-framework2

I was previously using a downloaded version of Zend Framework 2 and was basically able to do something like this:
// Set include paths (add Zend to the path)
set_include_path(get_include_path() . PATH_SEPARATOR . $__CONFIG['zendPath']);
// Setup the Zend Autoloader
require_once('library\Zend\Loader\StandardAutoloader.php');
$autoLoader = new StandardAutoloader(array(
'namespaces' => array(
'Zend' => $__CONFIG['zendPath'] . '/library/Zend'
)
));
This works because all Zend packages are inside the library folder. I now wanted to use composer to load only the Zend packages I need. The problem I'm running into is that the packages get arranged in the following way:
zendframework/zend-cache/Zend/...
zendframework/zend-loader/Zend/...
zendframework/zend-validator/Zend/...
etc.
I tried having separate namespace declarations in the StandardAutoloader like this:
$autoLoader = new StandardAutoloader(array(
'namespaces' => array(
'Zend\Cache' => $__CONFIG['zendPath'] . '/library/zend-cache/Zend/Cache',
'Zend\Loader' => $__CONFIG['zendPath'] . '/library/zend-loader/Zend/Loader',
'Zend\Validator' => $__CONFIG['zendPath'] . '/library/zend-validator/Zend/Validator'
)
));
That doesn't work. Am assuming the namespace probably can't have the backslashes in it? is there any way to make this work? Preferably without having to define each individual package.

You just have to fix the paths:
$libs = $__CONFIG['zendPath'];
$autoLoader = new StandardAutoloader(array(
'namespaces' => array(
'Zend\Cache' => $libs . '/library/zend-cache/Zend',
'Zend\Loader' => $libs . '/library/zend-loader/Zend',
'Zend\Validator' => $libs . '/library/zend-validator/Zend',
)
));
That should do the trick (it's un-tested, so I'm not sure about the trailing Zend

Let it autoload Zend for you:
$autoLoader = new StandardAutoloader(array(
'autoregister_zf' => true, // Auto load Zend Namespace..
'namespaces' => array(
// extra namespaces
)
));

Related

Zend Framework 2 autoloading hybridauth Library

I want to autoload Hybridauth external library in my module name "Account", I have included all libraries in the location
/var/www/ZendSkeletonApplication/module/Account/lib/hybridauth/Hybrid
Also in my module.php , I have the following namespaces
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__.'/src/'.__NAMESPACE__,
'hybridauth' => __DIR__.'/lib/hybridauth/Hybrid',
),
),
And in /var/www/ZendSkeletonApplication/module/Account/lib/hybridauth/Hybrid Auth.php file I have put the namespace.
namespace hybridauth;
class Hybrid_Auth {
......
In my controller I have declared the namespace as
use hybridauth\Hybrid_Auth;
But now whenever I make a call to new Hybrid_Auth( $config );, it shows error unable to load class.
I don't know what I am doing wrong here or weather I am following the correct way to autoload external libs in ZF2.
Any suggestion or Help !

What is the base directory for Zend ViewModel setTemplate?

I'm trying to load a different view depending on the value taken from a Regex route, but I can't get the ViewModel to load the template
public function viewAction()
{
$page = $this->params()->fromRoute()["page"];
$view = new ViewModel();
$view->setTemplate('module/Base/view/base/index/$page');
return $view;
}
I'm following the conventional module setup:
Base
|- config
| |- module.config.php
|- src
| |- Base
| |- Controller
| |- IndexController.php
|- view
|- base
| |- index
| |- index.phtml
| |- other.phtml
|- layout
And my view_manager configuration is:
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
I've tried all sort of values in the path passed to setTemplate, but I just keep getting the application error message, but no error log or details (that may be because I haven't figured out displaying errors properly). I have confirmed that $page is getting the name of the template correctly from the URL, and the template is in the correct directory.
So, where does ViewModel start looking for templates?
I've tried:
$view->setTemplate('$page.phtml');
$view->setTemplate('$page');
$view->setTemplate('base/index/$page.phtml');
$view->setTemplate('base/index/$page');
$view->setTemplate('module/Base/view/base/index/$page.phtml');
$view->setTemplate('module/Base/view/base/index/$page');
Each module that requires view scripts should have a view_manager key in module.config.php
You have two choices when registering view scripts.
'view_manager' => [
'template_map' => [
'module-name/foo/bar' => __DIR__ . '/../view/foo/bar.phtml',
'custom_view_template_name' => __DIR__ . '/../view/some/other/path/bar.phtml',
],
'template_path_stack' => [
__DIR__ . '/../view',
],
],
Template Path Stack
This represents a collection (stack) of paths that the view resolver will try to find paths from.
The view scripts that you set on the view models in the controller would then be relative to this directory path. This option saves you time by 'automatically' resolving view script paths using a common naming convention.
For example, if you have FooModule and the file FooModule/view/foo-module/index/index.phtml the template path that should be used in the controller would be foo-module/index/index.
Template Map
A one to one mapping of template name to template path. This means the template path is exactly the same as the array key.
You should try to favor this method as using the template_path_stack incurs a slight performance hit as the view scripts need to be resolved at runtime.
I am using it like this:
after the view folder inside your module add the path. So I assume yours is
view->setTemplate('base/index/$page');
This should do the trick.

make project multilingual with Zend/Translator

Good evening,
I want to add zend/translate to my project to show my webside in several languages. But there doesn't work anything. Here are the steps I done already:
In the module.config.php I looked if the translator is initialized:
...
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
...
'translator' => array(
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
...
In the Module.php in the Bootstrap I set the DefaultTranslator:
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
\Locale::setDefault('de_DE');
\Zend\Validator\AbstractValidator::setDefaultTranslator(
$e->getApplication()
->getServiceManager()
->get('translator')
);
}
But when I reload my webside there is a error like this:
Catchable fatal error: Argument 1 passed to Zend\Validator\AbstractValidator::setDefaultTranslator() must be an instance of Zend\Validator\Translator\TranslatorInterface, instance of Zend\I18n\Translator\Translator given, called in C:\xampp\htdocs\pimp\module\Application\Module.php on line 28 and defined in C:\xampp\htdocs\pimp\vendor\zendframework\zendframework\library\Zend\Validator\AbstractValidator.php on line 472
I think there is something I've forgotten..
Can someone help me?
Thanks.
There is a odd part in Zend i18n that Zend\I18n\Translator\Translator is not compatible with other components. The Mvc Translator binds the Translator to the other parts. The Zend\Mvc\I18n\Translator extends the "normal" translator and then it implements the translator interface requested by the Zend\Validator component.
So, create the Mvc Translator by using the MVC translator factory. Replace Zend\I18n\Translator\TranslatorServiceFactory with Zend\Mvc\Service\TranslatorServiceFactory.
this is simple tutorial
http://samminds.com/2012/09/zend-framework-2-translate-i18n-locale/
http://www.poedit.net/
but i have
,'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'translate',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
in module.config.php and a php file that all keys exist on it for example
<?php
/*
* General keys
*/
$this->translate('projectname');
$this->translate('login');
$this->translate('logout');
$this->translate('pages');
$this->translate('contents');
$this->translate('ourposition');
$this->translate('search_pleace');
$this->translate('contactform');
$this->translate('pleaseuploadresume');
?>
in poedit i only set path of this file. (help to better management) and on Catalog properties-> sources keywords translate should be exist ( and of course be better one) . any where i want show message . i write
echo $this->translate('key');

Zend Framework Multi Language Integration Steps

I am new to zend framework2 and I am working on a site with multi language integration. Please give me the idea about, how the builtin library and translation file should be configured, and how its can be called from view file
ZF2 has already integrated I18n tools.
How to integrate it
module.config.php
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
Files *.mo
Following previous step, create a folder and add your en_US.mo (for example) using Poedit (simple and good application)
Module.php
public function onBootstrap($e)
{
$translator = $e->getApplication()->getServiceManager()->get('translator');
$translator
->setLocale(\Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']))
->setFallbackLocale('en_US');
}
rq: Personally I use a session to stock my locale but it depends if I need a SEO using language
// session container
$sessionContainer = new Container('locale');
// test if session language exists
if(!$sessionContainer->offsetExists('mylocale')){
// if not use the browser locale
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
$sessionContainer->offsetSet('mylocale', Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']));
}else{
$sessionContainer->offsetSet('mylocale', 'en_US');
}
}
// translating system
$translator = $serviceManager->get('translator');
$translator ->setLocale($sessionContainer->mylocale)
->setFallbackLocale('en_US');
$mylocale = $sessionContainer->mylocale;
How to use it
In a view, just type that:
<?php echo $this->translate("Translate that!"); ?>
Some links to explore
http://samminds.com/2012/09/zend-framework-2-translate-i18n-locale/
http://framework.zend.com/manual/2.2/en/modules/zend.i18n.translating.html
http://framework.zend.com/manual/2.2/en/user-guide/styling-and-translations.html

Set translation for error messages in zend2

I struggle to set a default language for the error messages (displayed when trying to submit a invalid form) in Zend 2. I've downloaded the code from the quick start tutorial and added the following lines to ..module\Album\config\module.config.php:
//[...]
'translator' => array(
'locale' => 'de_DE',
'translation_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
and
$translator = $this->getServiceLocator()->get('translator');
$translator->setLocale('de_DE');
in my controller. Neither seems to work. There are some translations in ...\vendor\zendframework\zendframework\resources\languages. I don't have the Intl PHP extension installed, but I hope very much that the translation will work without this extension.
Thanks for your help,
Andreas
After some research, it appears that the Intl PHP Extension is really required. With the Intl Extension, you can set the default Translator to the abstract validator. From the docs:
$translator = new Zend\I18n\Translator\Translator();
$translator->addTranslationFile(
'phpArray',
'resources/languages/en.php',
'default',
'en_US'
);
Zend\Validator\AbstractValidator::setDefaultTranslator($translator);

Resources