Zend Framework 2 : Using Zend Framework 1 libraries
Do you still need some components of Zend Framework 1 in your Project that use Zend Framework 2 ? It easy to use Zend Framework 1 libraries in Zend Framework 2 by register Zend Framework 1 libraries in Zend Framework 2 StandardAutoloader or by register at composer’s ClassLoader.
1. By ZF2 StandardAutoloader
//we still need set include path because some component still use require_once others set_include_path(implode(PATH_SEPARATOR, array($Zf1Path, get_include_path()))); Zend\Loader\AutoloaderFactory::factory(array( 'Zend\Loader\StandardAutoloader' => array( 'autoregister_zf' => true, 'prefixes' => array( 'Zend_' => $Zf1Path.'/Zend' ), ) ));
2. By Composer’s ClassLoader
$loader = include 'vendor/autoload.php'; //we still need set include path because some component still use require_once others $loader->setUseIncludePath($Zf1Path); $loader->add('Zend_', $Zf1Path.'/Zend');
If you’re working with Zend Framework’s Skeleton Application, you should change init_autoloader.php like the following :
$Zf1Path = '/path/to/your/zf1/library'; // Composer autoloading if (file_exists('vendor/autoload.php')) { $loader = include 'vendor/autoload.php'; $loader->setUseIncludePath($Zf1Path); $loader->add('Zend_', $Zf1Path.'/Zend'); } $zf2Path = false; if (getenv('ZF2_PATH')) { // Support for ZF2_PATH environment variable or git submodule $zf2Path = getenv('ZF2_PATH'); } elseif (get_cfg_var('zf2_path')) { // Support for zf2_path directive value $zf2Path = get_cfg_var('zf2_path'); } elseif (is_dir('vendor/ZF2/library')) { $zf2Path = 'vendor/ZF2/library'; } if ($zf2Path) { include $zf2Path . '/Zend/Loader/AutoloaderFactory.php'; //we still need set include path because some component still use require_once others set_include_path(implode(PATH_SEPARATOR, array($Zf1Path, get_include_path()))); Zend\Loader\AutoloaderFactory::factory(array( 'Zend\Loader\StandardAutoloader' => array( 'autoregister_zf' => true, 'prefixes' => array( 'Zend_' => $Zf1Path.'/Zend' ), ) )); } if (!class_exists('Zend\Loader\AutoloaderFactory')) { throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.'); }
How about calling, just take a look like the following :
namespace Application\Controller; use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; class IndexController extends AbstractActionController { public function indexAction() { $date = new \Zend_Date; print_r($date->toArray()); return new ViewModel(); } }
offtopic. do you know how to disable a default validator like in array because with field modified on the fly i receive The input was not found in the haystack. sorry for posting here but on google i don’t find any good response.
The solution is not to remove/disable default validator. You should re-set the values of element on the fly too before setData. for example, in changing country, state will change via ajax ( that not in the haystack of the form element which has been created). so, you should re-set the value of the element on the fly too like the following :
I don’t know if this is a solution for case when you have more that one updated on the fly field depend each other on the last one. i will try to digg to find a solution to disable a validator for a specific field, now i put code like this $form->setValidationGroup(array(‘categorii’)); and onlyu the first select are validated.
maybe not the best answer, but I think my solution is the simplest way :). separate into other model/service if you don’t want a big controller.
I dont mean about how big data is but how to wrote this code to reset option values on the fly because ajax request is another request than actual request and that reset will be availabe for that request not this one.
[…] https://samsonasik.wordpress.com/2012/12/04/zend-framework-2-using-zend-framework-1-libraries-in-zend… […]
I need to use zf1 phpthumb library how do i use it in zf2 ?
you can set include path to the path of phpthumb :
and just use it 😉
Hi Samsonasik,
thanks for this tutorial.
I’ve set up a ZF2 project using zftool.phar, and my init_autoloader.php is slightly different within the “if ($zf2Path) {” statement:
if ($zf2Path) {
if (isset($loader)) { // add(‘Zend’, $zf2Path);
} else {
include $zf2Path . ‘/Zend/Loader/AutoloaderFactory.php’;
Zend\Loader\AutoloaderFactory::factory(array(
‘Zend\Loader\StandardAutoloader’ => array(
‘autoregister_zf’ => true
)
));
}
}
Commenting the “if (isset($loader)) {” did the trick, but I think it is there for a reason…
What do you think?
Thank you
um…, it for choice if we are working with composer or not.
aku lagi mau coba yang nomer 2 nya ClassLoader nih
sip
[…] For other strategies on how to use ZF1 in a ZF2 project, you can check out this blog post by Abdul Malik Ikhsan, Zend Framework 2 : Using Zend Framework 1 libraries. […]
Thanks for the great article! I want to use google+ authentication in my code so that people can easily login using their google+ account. Here is the link of the API:
https://github.com/google/google-api-php-client
it uses the prefix method and no namespaces. So I was wondering If I want to use it in my code without using composer how can I integrate it in my code?
Also I couldn’t figure out where exactly the standard autoloader is? Do you mean we should write this code in /vendor/Google/module.php? I really appreciate your time and help. You have an extremely helpful website.
the vendor files should be built by composer and not touch by us directly except we change the .gitignore part. if you want other path to save 3rd party, you can add it to other, and use ‘Zend\Loader\StandardAutoloader’ to register it.
Hello,
Thank you for the instructions.
I need to use ZF1 Models in my ZF2 project. However i can’t use getAdabter of ( ZF1 )
How can configure the data base adapter for ZF1 models in my ZF2 project?
ZF1 and ZF2 has different architecture. You need to inject DbAdapter into model to make it works in ZF2. Please read the documentation.