2009年2月27日星期五

Re: [fw-mvc] Proper use of Zend_Loader_Autoloader_Resource

-- Cristian Bichis <contact@zftutorials.com> wrote
(on Friday, 27 February 2009, 10:24 AM +0200):
> I tried last days to get in touch with some newer (or TBA) features of ZF. One
> of them is Zend_Loader_Autoloader_Resource.
>
> I made a sample for a modular archtecture. I am trying as a demonstration to
> load a Plugins model from /application/modules/default/models
>
> That's into application/Bootstrap.php:
>
> Zend_Loader::registerAutoload();
>
> $autoloader = new Zend_Loader_Autoloader_Resource(
> array(
> 'namespace' => 'Default',
> 'basePath' => MODULE_PATH
> )
> );
>
> $autoloader->addResourceTypes(array(
> 'Model' => array('path' => 'models', 'namespace' => 'Model'),
> 'DbTable' => array('path' => 'models/DbTable', 'namespace' =>
> 'DbTable')
> ));

Okay, two things here. First, if you're going to use the Resource
autoloader, don't uses Zend_Loader::registerAutoload(); use
Zend_Loader_Autoloader -- and the latter is implicit when you
instantiate the resource autoloader.

Second, based on the resource types you're defining, you probably want
to use the Zend_Application_Module_Autoloader variant, as it has all of
the resources you indicated above defined already.

In the end, it would look like this:

$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Default_',
'basePath' => MODULE_PATH,
));

and that's it.

> Into a controller plugin in am making use of resource autoloader this way:
>
> $plugins = new Default_Model_Plugins();
>
>
> Now here are my questions:
> 1. Why i am receiving this error:
>
> Warning: include(D:\_Work\site/application/modules/models//Plugins.php) [
> function.include]: failed to open stream: No such file or directory in D:\_Work
> \site\library\incubator\Zend\Loader\Autoloader\Resource.php on line 173
>
> I am not sure why is not included the "default" into path... Or anyway, what i
> am missing ?

First, getting rid of the Zend_Loader::registerAutoload() will likely
take care of this. Second, use Zend_Loader from current trunk, as it has
a few changes that make it work better with autoloading.

> 2. There is any other error ? Like namespace been 'namespace' => 'Default'
> or 'namespace' => 'Default_', as according to sample from incubator file
> ('namespace' => 'Stuff_').

I need to change that in the docblock -- no '_' suffix necessary with
resource autoloaders. The reason is that with resource autoloading,
we're making an assumption that the classes follow ZF coding standards
-- so the '_' is implied. (Zend_Loader_Autoloader, however, does require
the explicit '_' so that we can allow interop with autoloaders from
projects that do not follow the same conventions).

> 3. How should be the class name of the model ? Plugins ? Or Default_Plugins ?
> or Default_Model_Plugins ?

Default_Model_Plugins. Rule of thumb: the namespace you use is the first
segment, the resource type is the second segment, and then the class
name is the last.

/me makes a note to write documentation soon!

--
Matthew Weier O'Phinney
Software Architect | matthew@zend.com
Zend Framework | http://framework.zend.com/

没有评论: