2009年2月27日星期五

Re: [fw-mvc] Proper use of Zend_Loader_Autoloader_Resource

Thanks.

Any idea which would be the faster (as performance) approach ?

$autoloader->registerNamespace('Imagis');
or
$autoloader->setFallbackAutoloader(true);
?
-- Cristian Bichis <contact@zftutorials.com> wrote (on Friday, 27 February 2009, 10:13 PM +0200):   
Hmmm,  I noticed one potential problem with autoloader. It might my fault anyway...  Seems i can't load some non Zend classes from library now after i switched to Resource autoloader.     
 Zend_Loader_Autoloader is a namespace based autoloader -- this allows it to bail early if a namespace is not found -- this is particularly useful if you have other autoloaders in play.  You can enable additional namespaces easily:      $autoloader->registerNamespace('Imagis');  You can also tell the autoloader to be a "fallback" autoloader -- i.e., if the namespace isn't registered, it will still attempt to load it:      $autoloader->setFallbackAutoloader(true);     
index.php:     set_include_path(         BASE_PATH . '/library/dasprid' . PATH_SEPARATOR         .BASE_PATH . '/library/incubator' . PATH_SEPARATOR         .BASE_PATH . '/library' . PATH_SEPARATOR         . get_include_path()     );  Bootstrap:          require 'Zend/Loader/Autoloader.php';         require 'Zend/Loader/Autoloader/Resource.php';                 $loader = new Zend_Loader_Autoloader_Resource(array(              'namespace' => 'Default_',              'basePath'  => MODULE_PATH         ));                 $loader->addResourceType('Model', 'models', 'Model');        //i used resource autoloader since i would add also forms, filters, aso        .................................................         $acl = new Imagis_Acl($auth);        //class Imagis_ACL is obviously into library/Imagis   (class Imagis_Acl extends Zend_Acl)   But... I am receiving this:   Fatal error: Class 'Imagis_Acl' not found in D:\_Work\site\application\ Bootstrap.php on line 174  Not sure, might be my fault...    -- Best regards, Cristian Bichis www.zftutorials.com | www.zfforums.com | www.zftalk.com | www.zflinks.com        -- Cristian Bichis <contact@zftutorials.com> wrote     (on Friday, 27 February 2009, 07:01 PM +0200):           Thanks for reply, my code works now... :)          One more question. How should i make the system automatically         recognize all my modules ?             I know this is also related to Zend_Application prepared by Dasprid, i         am wondering if this is possible (using just autoloader) until his         component is released (is not moved to incubator now).       Loop through your modules, and instantiate an autoloader for each:          $loaders = array();         foreach ($front->getControllerDirectory() as $module => $dir) {             $loaders[$module] = new Zend_Application_Module_Autoloader(array(                 'namespace' => ucfirst($module),                 'basePath'  => dirname($dir),             ));         }      You can also grab Zend_Application from svn:          http://framework.zend.com/svn/framework/standard/branches/dasprid/Zend_Application      BTW, Zend_Application is being hacked on by more than just DASPRiD -- if     you have ideas or feedback, please feel free to comment.            Matthew Weier O'Phinney wrote:              -- 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!              --         Best regards,         Cristian Bichis         www.zftutorials.com | www.zfforums.com | www.zftalk.com | www.zflinks.com           
   


--  Best regards, Cristian Bichis www.zftutorials.com | www.zfforums.com | www.zftalk.com | www.zflinks.com

没有评论: