开发者

Zend Framework Autoloader question

开发者 https://www.devze.com 2023-02-13 09:30 出处:网络
In zend framework I register my namespace like this (in application.php): \'autoloaderNamespaces\' => array(

In zend framework I register my namespace like this (in application.php):

'autoloaderNamespaces' => array(
    'Cms_'
)

And after this - I'd expect that Zend would always check that path in addition to Zend and ZendX paths if unknown class is called. But for some reason this doesn't work with for example view helpers.

I still have to register a separate path for my view helpers even though view helper scripts are named according to Zend coding standards and are located in:

Cms/View/Helper/

And this is how I register helper path in config file:

view' => array(
    'charset' => 'UTF-8',
    'doctype' => 'XHTML1_TRANSITIONAL',
    'helperPath' => array(
        'Cms_View_Helper_' => 'Cms/View/Helper'
        )
),

So - I'm not sure why I have to register "Cms" namespace twice first through 'autoloaderNamespaces' and then through View "helperPath"? Shoul开发者_如何学JAVAdn't Cms namespace include Cms/View/Helper namespace?

can someone plz clarify this:)


View Helpers are considered application specific, so in the Recommended Project Directory Structure View Helpers are supposed to be placed in application/views/helpers. Which means, they usually wouldn't be found if ZF would just resolve the conventionalized class name.

When you call helpers with $this->helperName() or $this->getHelper('HelperName') from the View, the View will use the PluginLoader with the configured prefix and path to fetch that helper and inject the current View Instance. See sourcecode for all the details:

  • http://framework.zend.com/svn/framework/standard/trunk/library/Zend/View/Abstract.php
  • http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Loader/PluginLoader.php

So in other words, when loading a ViewHelper, you are not using the Autoloader. See:

  • Loading Plugins in the Zend Framework Reference Guide


This is taken directly from one of my application.ini files.

autoloaderNamespaces.Foo = "Foo"
includePaths.library = APPLICATION_PATH "/../library"

My "Foo" libraries are in the library directory - library/Foo. All I've done up until this point is make the "Foo" library available within the include paths.

I need to add a separate helper path to the default list for my view, otherwise the view won't look in that directory for matching view helpers. I think of loading view helpers as direct discovery. The view needs explicit instructions on where to look for helpers.


I believe it is exactly as you describe, the documentation on custom view helpers is pretty explicit about it:

You may, and should, give the class name a prefix, and it is recommended that you use 'View_Helper' as part of that prefix: "My_View_Helper_SpecialPurpose". (You will need to pass in the prefix, with or without the trailing underscore, to addHelperPath() or setHelperPath()).

This does make some sense to me though. In theory you could build a library of generic view helpers that could be re-used across multiple applications, so binding them to a specific application namespace would be inconvenient, i.e. if all my helpers were prefixed 'MyApp_' I would have to rename them to be able to use them in 'MyOtherApp'.

0

精彩评论

暂无评论...
验证码 换一张
取 消