Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to indicate zend framework where my custom classes are

I have a folder with custom classes in a ZF 1.10 application. The folder is located in /library. How can I tell ZF where they are? Both application.ini and index.php set the path to the library but then ZF can't find the files.

Thank you

like image 659
curro Avatar asked Mar 22 '10 20:03

curro


2 Answers

We often come across a problem of writing our own custom functions or classes and where to place them.

So to add custom class (or custom library) one can use zend framework's autoloader namespaces.

Add the below line in application.ini file

autoloaderNamespaces.custom = "Custom_"

OR

autoloaderNamespaces[] = "Custom_"

All the custom classes will be kept under library directory. Create a folder name 'Custom' (which is defined in application.ini) in the library directory.

Classes will be prefixed with 'Custom_' at declaration in the file (e.g. Custom_Test)

Now we can use this class as $test = new Custom_Test(), in our application.

like image 105
Muhammad Yaseen Avatar answered Oct 21 '22 04:10

Muhammad Yaseen


There are many possible solutions. The most common, when using Zend Application, is to register the namespace in application.ini by adding:

autoloaderNamespaces[] = "Example_"

Other solutions:

  • Add your dir to include_path using set_include_path() (ad hoc solution)
  • Follow PEAR naming conventions (so the path resolving was possible)

Set up autoloader in Bootstrap.php:

protected function _initAutoloader()
{
    $autoloader = Zend_Loader_Autoloader::getInstance();
    $autoloader->registerNamespace("Example"); // or Example_
}

Eventually, set up module or resource autoloader, eg.

$resourceLoader->addResourceTypes(array(
     'acl' => array(
        'path'      => 'acls/',
        'namespace' => 'Acl',
    ),
    'example' => array(
        'path'      => 'examples/',
        'namespace' => 'Example',
    ),        
));
like image 30
takeshin Avatar answered Oct 21 '22 05:10

takeshin