where to save custom autoloaders in zend?

2019-05-01 04:02发布

I am trying to use phpThumb in my application by making a custom autoloader.

class My_Loader_Autoloader_PhpThumb implements Zend_Loader_Autoloader_Interface {

   static protected $php_thumb_classes = array(
      'PhpThumb'        => 'PhpThumb.inc.php',
      'ThumbBase'       => 'ThumbBase.inc.php',
      'PhpThumbFactory' => 'ThumbLib.inc.php',
      'GdThumb'         => 'GdThumb.inc.php',
      'GdReflectionLib' => 'thumb_plugins/gd_reflection.inc.php',
   );

  /**
   * Autoload a class
   *
   * @param   string $class
   * @return  mixed
   *          False [if unable to load $class]
   *          get_class($class) [if $class is successfully loaded]
   */
   public function autoload($class) {
      $file = APPLICATION_PATH . '/../library/PhpThumb/' . self::$php_thumb_classes[$class];
      if (is_file($file)) {
         require_once($file);
         return $class;
      }
      return false;
   }
}

i saved this file as PhpThumb.php in the loaders/Autoloader folder. Then added this line to the bootstrap file:

Zend_Loader_Autoloader::getInstance()->pushAutoloader(new My_Loader_Autoloader_PhpThumb());

But it produces an error saying the class was not found. I am guessing that the 'CustomLoader_PhpThumb.php' needs to be saved somewhere else. Any idea guys ?


Update1:

Bootstrap.php file contents

   <?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initAutoload()
    {
        $autoLoader=Zend_Loader_Autoloader::getInstance();
        $resourceLoader=new Zend_Loader_Autoloader_Resource(array(
            'basePath'=>APPLICATION_PATH,
            'namespace'=>'',
            'resourceTypes'=>array(
                'form'=>array(
                    'path'=>'forms/',
                    'namespace'=>'Form_'
                ),
                'models'=>array(
                    'path'=>'models/',
                    'namespace'=>'Model_'
                ),                
            )

            ));

        //return $autoLoader;

        $resourceLoader->addResourceType('loader', 'loaders/', 'My_Loader_');

        $autoLoader->pushAutoloader($resourceLoader);
        $autoLoader->pushAutoloader(new My_Loader_Autoloader_PhpThumb());
    }


}

?>

3条回答
劫难
2楼-- · 2019-05-01 04:42

I did it in another way, long more easy, without any autoloader, just using "My".

1.- Create a phpThumbFolder with all the files of the library into project/library/My/PhpThumb

2.- Create a file named PhpThumbFactory.php into that folder with the following content:

require_once('ThumbLib.inc.php');

class My_PhpThumb_PhpThumbFactory
{
   public static function create ($filename = null, $options = array(), $isDataStream = false)
   {
       return PhpThumbFactory::create($filename, $options, $isDataStream);
   }
}

3.- Enjoy:

public function editimageAction()
{
    //rutaImg is the physical path to my images defined in application.ini
    $rutaImg = Zend_Registry::get('config')->rutaImg. "test.jpg";

    //Call to our "gateway" class
    $thumb = My_PhpThumb_PhpThumbFactory::create($rutaImg);
    //Resize
    $thumb->adaptiveResize(20, 20);
    //Show
    $thumb->show();

    $this->_helper->viewRenderer->setNoRender();
    $this->_helper->layout->disableLayout();
}

I hope it can help anyone ;)

P.D. The "gateway" class is to avoid modifying any code of the library, so any possible update is easy.

查看更多
冷血范
3楼-- · 2019-05-01 04:44

PhpThumb.php should be saved to library/CustomLoader folder, and you should setup autoloader for this class also, for example in application.ini:

autoloaderNamespaces[] = "CustomLoader_"

or in Bootstrap.php:

$autoloader->registerNamespace('CustomLoader_');

I couldn't try, I hope it works!

查看更多
爷的心禁止访问
4楼-- · 2019-05-01 05:00

I'm also using PhpThumb and the same autoloader. In my case however it is called My_Loader_Autoloader_PhpThumb and is located in APPLICATION_PATH . '/loaders/Autoloader/PhpThumb.php.

In my Bootstrap.php, first I load the loaders path to the Zend_Loader_Autoloader and then I push the My_Loader_Autoloader_PhpThumb autoloader as follows:

    $autoLoader = Zend_Loader_Autoloader::getInstance();

    $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
                'basePath' => APPLICATION_PATH,
                'namespace' => '',
            ));


    $resourceLoader->addResourceType('loader', 'loaders/', 'My_Loader_');

    $autoLoader->pushAutoloader($resourceLoader);
    $autoLoader->pushAutoloader(new My_Loader_Autoloader_PhpThumb());

Hope this will help.

查看更多
登录 后发表回答