ZF Include path

后端 未结 2 1619
再見小時候
再見小時候 2021-01-15 20:16

Is it correct to require_once? where and how would you put it include path?

Should it not be in a application.ini or bootstrap?

EXAMPLE:

req         


        
相关标签:
2条回答
  • 2021-01-15 20:26

    Generally speaking, you can avoid require_once calls almost entirely by appropriately using Zend_Loader_Autoloader. Of course, the key is "appropriate".

    Typically, your public/index.php sets the include_path to be the library folder. Then, if you are using Zend_Application, the Zend_Loader_Autoloader is registered to find any PSR-0 compliant classes whose namespace prefixes have been registered using the autoloadernamespaces array in application/configs/application.ini.

    The tricky part is for classes defined in files that don't "reside on the include_path", like models that appear in application/models, services that reside in application/services, etc. Even though the classes defined there tend to follow PSR-0 standards, the fact that the PSR-0 mapping occurs relative to a base off the include-path means that the system has to know the mapping between classname prefixes and base paths. This is where resource autoloaders come in. These resource autoloaders are typically set up automatically in the application Bootstrap extending Zend_Application_Bootstrap_Bootstrap and module bootstraps that extend Zend_Application_Module_Bootstrap.

    View helpers are another example of classes that reside "off the include_path", perhaps in something like application/views/helpers. Since these are typically invoked in a view script using a short form $this->someHelper($someParam), the system must be told how to generate the fully qualified classname from this short name. This is accomplished using $view->addPrefixPath() which maps namespace prefixes to filesystem locations. Again, the app-level and module level bootstrapping mechanism sets most of these up for you.

    For libraries/classes that do not follow PSR-0 standards, you can create custom autoloaders and attach them (typically at Bootstrap) to the Zend_Loader_Autoloader singleton. This is the only place where you would have an explicit include/require.

    tl;dr: With proper use of the existing ZF autoloader mechanism, you almost never need to have include/require statements in your own application code.

    0 讨论(0)
  • 2021-01-15 20:33

    It is not correct in this case.

    First off, please use Zend Tool. It will create the files you don't know how to create yourself. It will create the correct class names, extend them appropriately and require_once anything that might be needed.

    Do not place require_once in the bootstrap. You want it to execute only when needed, not with every request.

    As for the example you've provided, the correct version would be:

    require_once "Zend/View/Interface.php";
    class Zend_View_Helper_Foo extends Zend_View_Helper_Abstract {
    }
    

    The class that is extended by the helper is autoloaded and putting it in require_once does nothing.

    0 讨论(0)
提交回复
热议问题