How can I reduce my Class Name in Zend to call it from any where in application?

老子叫甜甜 提交于 2019-12-06 14:46:43

You have to write Your own autoloader in bootstrap:

  protected function _initAutoload()
  {
    $autoLoader = Zend_Loader_Autoloader::getInstance(); 
    $autoLoader->registerNamespace('My_'); 
    $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
        'basePath'  =>  APPLICATION_PATH,
        'namespace' =>  '',
        'resourceTypes' =>  array(
            'form'  =>  array(
                'path'  =>  'forms/',
                'namespace' =>  'Form_'
            ),
            'model' =>  array(
                'path'  =>  'models/',
                'namespace' =>  'Model_'
            )
        )
    ));
    return $autoLoader;
  }

This will also register namespace My_ for Your own libraries

that is the way it will work with me

    set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/models'),
    get_include_path(),
)));

then when u just include the file

./
/application
 /models
   /x.php
 ...

just include"x.php" and it will work

Try adding this line into your configs/application.ini

autoloaderNamespaces[] = "Models"

I also don't like the very long model names in ZF i.e. class Application_Model_DbTable_SomeTableName

Retaining Pear-style naming conventions and auto discovery... this is how I solved it:

1: add the following to your index.php include_path: realpath(APPLICATION_PATH.'../models') //depends on where your index.php file is located.

2: add this line to your application.ini autoloaderNamespaces[] = "Model_"

3: create a folder inside your models folder and name is Model

4: name your model class like this Model_SomeTableName and save all your model classes in the new models/Model folder.

now in your controller you can simply call $m = new Model_SomeTableName(); and it will be automatically found and included.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!