3
votes

The things I did is

  1. zf create project demo1 in command prompt
  2. add the lines to application.ini
    • appnamespace = "Application"
    • resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
  3. add a layout with header and footer using partial() (They are perfectly worked)
  4. create Data.php in models directory and add this simple class

    <?php class Application_Model_Data{   }//Application for appnamespace 
    
  5. then I tried to load this class(by creating instance)from index controller index action

    $data = new Application_Model_Data();

  6. but when I test it even in this level it gives an error

    Fatal error: Class 'Application_Model_Data' not found in C:\Zend\...\IndexController.php

Question

  1. Do I want to add a autoloader to load models in the application( I'm not used modules)
  2. if not what was I missed to add

please help I'm stuck in the beginning,Thank you

5
What version of ZF do you have? I did the quickstart and It worked perfectlycurro
Yes,It's must be worked.Zend Server CE (PHP 5.3)Zend Framework Version 1.9.5.I think it's a problem of my code or the system..ThanksDumindu

5 Answers

4
votes

this should work!!

add this function to bootstrap:

protected function _initResourceAutoloader()
{
     $autoloader = new Zend_Loader_Autoloader_Resource(array(
        'basePath'  => APPLICATION_PATH,
        'namespace' => 'Application',
     ));

     $autoloader->addResourceType( 'model', 'models', 'Model');
     return $autoloader;
}
2
votes

You need to setup a resource Autoloader in your Bootstrap, something like this:

protected function _initResourceAutoloader()
{
     $autoloader = new Zend_Loader_Autoloader_Resource(array(
        'basePath'  => 'path/to/application/directory',
        'namespace' => 'Application_',
     ));

     return $autoloader;
}

With that, Zend can load the modules in your application, and just not models, but DbTable, Forms, Plugins, etc.

2
votes

write the following in your bootstrap file:

protected function _initDefaultModuleAutoloader()
{ 

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

    return $resourceLoader;

}

in your models folder create new file and name it "Data.php"

in the Data.php declare the class like this:

class Model_Data extends Zend_Db_Table_Abstract {.....}

you can now instantiate your data model like so:

$data = new Model_Data();

good luck :-)

0
votes

in your application ini you should have autoloadernamespaces.0 = 'Application' instead of appnamespace

then your model would be in

/library/Application/Model/Data.php

but why dont you use the default "models" folder in the suggested application structure.

0
votes

I had forgotten to add the file extension .php to the file, just in case someone else makes the same mistake