How to indicate zend framework where my custom classes are
Asked Answered
A

4

6

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

Anorexia answered 22/3, 2010 at 20:46 Comment(0)
G
8

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',
    ),        
));
Gilding answered 22/3, 2010 at 22:15 Comment(2)
Thank you takeshin, it worked! The "$autoloader->registerNamespace(.." dit it!Anorexia
Hi, pretty old post to ask this but still can you help me with this? Zend (1.12) I have a Directory Example and a class Example_Test directly under Example. I registered a namespace Example and then when I access the class Example_Test in my php code I can do that without any problem. But when I look at the method getClassPath() inside Zend_Loader_Autoloader_Resource it does not return a valid path(coz of some conditions), so this means the file is not loaded by the method autoload(). Then how is it actually getting loaded?Intendancy
A
24

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.

Aquino answered 3/1, 2011 at 10:11 Comment(2)
For those who are looking at this, I think this method is a bit more preferred, than the accepted answer.Sea
I agree with Angl.King.47 and Muhammad Yaseen - In my own experience, this is a much simpler and more efficient way of including your own class libraries. Additionally, the standard index.php in zend will include the library directory in the PHP include path.Nikolos
G
8

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',
    ),        
));
Gilding answered 22/3, 2010 at 22:15 Comment(2)
Thank you takeshin, it worked! The "$autoloader->registerNamespace(.." dit it!Anorexia
Hi, pretty old post to ask this but still can you help me with this? Zend (1.12) I have a Directory Example and a class Example_Test directly under Example. I registered a namespace Example and then when I access the class Example_Test in my php code I can do that without any problem. But when I look at the method getClassPath() inside Zend_Loader_Autoloader_Resource it does not return a valid path(coz of some conditions), so this means the file is not loaded by the method autoload(). Then how is it actually getting loaded?Intendancy
R
1

Check out this older Zend Framework tutorial from Rob Allen, specifically on page 4, where he talks about the bootstrapper. His newer tutorials, as excellent as they are, appear to rely on Zend Tool to do the application creation and gloss over this.

One thing that alarmed me, however, was that you mentioned that the folder you're trying to include is public/library. Unless you intentionally want to share your code with the world, I would strongly suggest you place it elsewhere...unless you've got a different "public" folder not shared with the public (in which case you may want to consider renaming it to avoid future confusion).

Rudyrudyard answered 22/3, 2010 at 20:55 Comment(4)
Thank you, Cal. Sorry, I don't have the library in the public directory. It's just a mistake. It's in the "root" directory.Anorexia
Rob Allen tutorial relies on ZF 1.5. The version I'm using is 1.10. Although it creates a Bootstrap file, the Bootstrap class is empty. Everything works fine except for the custom classes. I have the ZF library in another place and have PHP include path point to it.Anorexia
The meat and potatoes of it should be set_include_path, which I would think you could at least place in the empty bootstrap class. Also, Rob has updated tutorials through 1.10 on his site.Rudyrudyard
the "older" tutorial seems to be dead ;)Stimulate
N
0

Add your custom library to composer.json:

"autoload": {
"psr-0": {"Your": "vendor/My/library"}
},

and run composer update

Nurserymaid answered 6/5, 2014 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.