Reputation: 1347
I have under my /application folder a folder named "grids". Now my classnames are App_Grid_CustomGrid. How can I autoload these classes under the grids folder? I have tried the follow code in index.php before the Zend_Application. But it didn't work. The code couldn't find the CustomGrid class.
require_once 'Zend/Loader/Autoloader/Resource.php';
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
));
$resourceLoader->addResourceType('grid', 'grids', 'Grid');
Upvotes: 0
Views: 556
Reputation: 4173
I've used the following method in Bootstrap.ini to autoload custom classes stored in a folder in my application root (customised to suit your needs):
protected function _initNamespaces()
{
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
'resourceTypes' => array(
'grid' => array(
'path' => 'grids/',
'namespace' => 'Grid',
),
'example' => array(
'path' => 'example_folder/',
'namespace => 'Example'
)
));
}
Leaving the namespace blank allows for the you to omit App_
from the start of your class names. I do this as seperately, I use App_
in the Library folder for custom Classes and Plugins. Multiple namespaces can be loaded using the above method, you just add additional arrays to the resourcesType
argument.
The application structure looks like this:
|Project
|-Application
|-grids
|-Test.php
|-configs
|-controllers
|-models
|-views
|-Bootstrap.php
|-Docs
|-Library
|-Public
|-.zfproject.xml
Test.php would look like this:
<?php
class Grid_Test
{
/**
* Return sum of two variables
*
* @param int $x
* @param int $y
* @return array
*/
public function add($x, $y)
{
return $x + $y;
}
}
Upvotes: 2
Reputation: 69977
Try:
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => 'App',
));
$resourceLoader->addResourceType('grid', 'grids/', 'Grid');
I think it wasn't working because it needs the namespace since it is prefixed with App_
, and there was no trailing slash on the end of the directory so it may have been looking for gridsCustomGrid.php
as well.
Upvotes: 1