Welcome to Abdul Malik Ikhsan's Blog

Zend Framework 2 : Using Zend Framework 1 libraries

Posted in Tutorial PHP, Zend Framework, Zend Framework 2 by samsonasik on December 4, 2012

zf2-zendframework2 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();
    }
}
About these ads

13 Responses

Subscribe to comments with RSS.

  1. Dragos said, on December 6, 2012 at 12:57 am

    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.

    • samsonasik said, on December 6, 2012 at 1:18 am

      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 :

      if ($this->request->isPost()) {
          $form->setInputFilter($register->getInputFilter());
          $states  = $this->getStateTable()->getState($this->request->getPost('country_id'));
          $Newstatecollection[] = 'Select your State';
          foreach($states as $state)
         {
               $Newstatecollection[$state->state_id] = $state->state_name;
          }
          $form->get('state_id')
                       ->setOptions(
                        array('value_options'=> $Newstatecollection)) ;
      
  2. Dragos said, on December 6, 2012 at 1:33 am

    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.

    • samsonasik said, on December 6, 2012 at 1:38 am

      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.

  3. Dragos said, on December 6, 2012 at 6:50 pm

    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.

  4. rajendra said, on March 7, 2013 at 3:15 pm

    I need to use zf1 phpthumb library how do i use it in zf2 ?

  5. samsonasik said, on March 7, 2013 at 5:07 pm

    you can set include path to the path of phpthumb :

    set_include_path(implode(PATH_SEPARATOR, array(realpath('/path/to/phpthumb'),get_include_path())));
    

    and just use it ;)

  6. Sergio said, on May 21, 2013 at 8:06 pm

    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

    • samsonasik said, on May 22, 2013 at 2:48 am

      um…, it for choice if we are working with composer or not.

  7. Vertical blind said, on December 17, 2013 at 10:24 am

    aku lagi mau coba yang nomer 2 nya ClassLoader nih

  8. […] 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. […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 247 other followers

%d bloggers like this: