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

匿名 (未验证) 提交于 2019-12-03 09:06:55

问题:

I want to follow Zend file naming convention.

I have Object.php file with a class and a function in it.

MyZendProject->application->models->Test->Object.php

class Model_Test_Object {         public function test() {     echo "Test";     } }  

Now I want to access above test function in following file but it is not finding it.

MyZendProject->application->modules->test->controllers->TestController.php

$testModel = new Model_Test_Object(); $testModel->test(); 

But when I name the class Application_Model_Test_Object in Object.php and call it in TestController like this then it works.

$testModel = new Application_Model_Test_Object(); $testModel->test(); 

Problem is that I don't want to use Application in class name because it is too long and ugly. Is there any way that I name the class Model_Test_Object and access it anywhere in application without including any file. I have seen somewhere in Botstrap.php to solve this issue but I did not remember it and unable to find it again.

Any help will be appreciated.

Thanks

回答1:

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



回答2:

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



回答3:

Try adding this line into your configs/application.ini

autoloaderNamespaces[] = "Models"



回答4:

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.



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