1

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

Charles
  • 50,943
  • 13
  • 104
  • 142
Awan
  • 18,096
  • 36
  • 89
  • 131
  • I guess you've an autoloader running? Which? Please show how you configure it. – rik Dec 19 '10 at 13:38
  • @rik: I am configuring it first time. Can you tell me that where will I find autoloader configuration to show you. – Awan Dec 19 '10 at 13:41

4 Answers4

3

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

Adam
  • 873
  • 7
  • 33
  • Thanks Adam. It is working now with `Model_Test_Object` name. But I did not understand the purpose of **My_**. Where can I use this? – Awan Dec 19 '10 at 15:16
  • In library directory You have Zend subdir and Zend_CLASS-NAME directories in it, there are all Zend classes. You can create Your own plugins, components etc. in Your directory. This script creates namespace My_ so You can create library/My/Plugin/Plugin.php and access it by $foo = new My_Plugin_Plugin(). This is helpful with for example Acl plugins or other custom functionality that You want to have in every ZF project. – Adam Dec 19 '10 at 15:26
  • Ok. It is working for default module. But models and forms for other modules are not accessible in even in their own module's controllers. Can you provide some more detail to do this in other modules as well. I have a question for this also: http://stackoverflow.com/questions/4891403/unable-to-access-module-forms-in-module-controller – Awan Feb 05 '11 at 15:55
1

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

shereifhawary
  • 463
  • 4
  • 13
  • Where will I place your above code. And I want to place my **Object.php** file in `zendproject/application/models/Test/`. – Awan Dec 19 '10 at 13:47
  • Your solution is little inflexible and in my opinion not follow ZF coding standards – Adam Dec 19 '10 at 15:11
  • @Awan in the public/index.php @Adam this is the way ZF user to include file in the class lib !! – shereifhawary Dec 20 '10 at 04:31
1

Try adding this line into your configs/application.ini

autoloaderNamespaces[] = "Models"

TuK
  • 3,556
  • 4
  • 24
  • 24
1

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.