My directory structure is like this:
- c:\Workspaces\Zend
- c:\Workspaces\Custom library
Custom library is a shared library, which is in use in other applications. It doesn't use namespaces, just old style underscores.
I downloaded the ZF2-Restful-Module-Skeleton which i intend to use as a restful server. In the InfoController I have this code:
namespace Main\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
class InfoController extends AbstractRestfulController
{
public function getList()
{
$data = array(
'phone' => '+30123456789',
'email' => 'email@domain',
);
$Res = new CL_Res_Chain_Mutable();
return $data;
}
}
Error message is:
Fatal error: Class 'Main\Controller\CL_Res_Chain_Mutable' not found in C:\Workspaces\Zend\module\Main\src\Main\Controller\InfoController.php
Obviously, I need to add this custom library to my Zend application, but Im "little" lost here, I really don't know how to do this. I have googled couple solutions, but none of them seem to be like this.
Also, I have another library in folder c:\Workspaces\Custom library 2
, which has (among other files) file(class) D.php, which I have used like D::dump($data);
How can I get it to work in my Zend application like that?
The namespace of your class is
Main\Controller
. If you instanciate a new class herenew CL_Res_Chain_Mutable()
php will load it relative to the current namespaceMain\Controller\CL_Res_Chain_Mutable
. Your class is not a namespaced class so you need to load it from the root. Just put a \ in frontnew \CL_Res_Chain_Mutable()
.By default your application will be using the Standard Autloader (PSR-0). This will find your files based on a namespaces, and a naming convension used by ZF2. ZF2 will allow you to register multiple Autoloaders, so you can use different strategies, which is what you will need to do, here's an example:
Module.php
This setup will use tell ZF2 to check the class map first, if it can't find what it's looking for it will revert to the standard autoloader.
http://framework.zend.com/manual/2.0/en/modules/zend.loader.class-map-autoloader.html
You need to configure the
StandardAutoloader
to load your library classes. The easiest way is to modify the Application module'sModule::getAutoloaderConfig()
method so that it looks something like this:I've added a
prefixes
key and then listed the prefix name and where to find it on disk. The Standard Autoloader documentation has more details.If you are working with a Zend Skeleton Application you may also simply add these namespaces to your init_autoloader.php file.