0
votes

I have a simple ZF application (no modules) which works fine when I require classes before I use them. However, I'd like to use the ZF autoloader to load them automatically (which I assumed was default behaviour).

How would I go about doing this? I'm a bit confused by the new(ish) Zend_Application way of doing things. My directory structure is the standard one:

application/
  controllers/
  models/
  views/        
  scripts/
    Bootstrap.php

For example removing the require in this method:

class HomeController extends Zend_Controller_Action {
    public function indexAction() {
        $tasks = array();

        //require 'models/Task.php';
        $tasks[] = new Application_Model_Task(array(...));
    }
}
1
Did you use "zf" command line tool to create the application or did you follow the usual directory structure and create items manually?Alistair

1 Answers

3
votes

If you use Zend Frame Version 1.8 (+) put this in your Bootstrap.php:

   /**
     * Initialize the autoloader
     *
     * @return Zend_Application_Module_Autoloader
     */
    protected function _initAutoload()
    {
        $autoloader = new Zend_Application_Module_Autoloader(array(
                'namespace' => 'My',
                'basePath'  => dirname(__FILE__),
        ));

        return $autoloader;
    }

And now try :

$model = new My_Model_Task();