0

I'm using zend framework2 skeleton, and I'm trying to add my own classes behind vendor folder. The problem is: I don't know how to prepare the classes to be loaded on demand (autoloaded) only when I'll instantiate one of them from any module.

I tried with the solution explained at: How to load a custom library in Zend Framework 2? , but I'm getting the same error:

Fatal error: Class 'myNamespace\myClass' not found in path\IndexController.php on line x

My question is: Where and how should I insert my own classes to be later used from my modules?

1
  • If you want to put classes in the vendor folder you need to create a module for it or use another one already there then you can use the namespace of the new module or the reused module to access the class. For Zend to know where to look you have to tell it about the module in application.config.php then the module's Module.php must be setup. If you can provide some more information about what you are trying to include I may be able to elaborate. Commented Nov 22, 2013 at 22:30

2 Answers 2

1

Firstly, if they are your own classes and directly related to the modules (i.e not some custom library you have written) then you are better off having them in the actual module, not in the vendor folder.

However, if you are trying to include your own library that you have written I'd suggest you consider storing this elsewhere and adding it as a dependancy via composer.

I don't recommend this but you should be able to get the autoloading working by using the psr0 autoloader built in to composer (assuming your classes follow psr0). Example composer.json config:

{
    [...]
    "autoload": {
        "psr-0": {
            "MyNameSpace": "path/to/root/src/directory"
        }
    }
    [...]
}
Sign up to request clarification or add additional context in comments.

Comments

0

I know its a bit old but i was facing the same issue so what i did was:

inside vendor folder i created a new folder and gave it the name of Custom. Inside that folder i added my class file (MyClass.php).

inside MyClass.php i added this line (namespace Zend\Custom;).

And inside my Controller at the top use Zend\Custom\MyClass;

and within a method of that controller

$someVar = new MyClass();

$someVar->someClassMethod();

I hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.