How to add new class and autoload in zend framewor

2019-02-20 04:55发布

问题:

I am new on Zend framework and using first time it. I am looking for simple basic tutorials which I can read in very short time. I also stuck on if I want to add new class in Zend library. And it should also auto load when I make any new controller.

Please give your opinions if you have.

Regards,

回答1:

This helped me At the beginning:

http://www.zendcasts.com/

http://devzone.zend.com/search/results?q=autoload (just search)

As autoload your class, This is the my way:

Create folder 'My' into library/ in it create folder 'Utils' and in Utils file 'Utils.php' so the path is library/My/Utils/Utils.php

For this path You must call class: class My_Utils_Utils{ ... }

and in configs/application.ini Put

appnamespace = "Application"

autoloaderNamespaces.my = "My_"

Then you can use namespace My_ and class My_Utils_Utils

In controller: $test = new My_Utils_Utils();



回答2:

  • I am looking for simple basic tutorials

    Here are a few tutorials I found while googling:

    1. Official quickstart tutorial
    2. A great book by frequent ZF-contributer Padráic Brady: Survive the deep end!
    3. http://akrabat.com/zend-framework-tutorial/
    4. Page with different tutorials: ZFTutorials.com
  • I also stuck on if I want to add new class in Zend library

    You should not add new classes to the library per se, but instead create your own library or add classes in the "models"-folder/folders (if you use the modular project layout). Autoloading is achieved by utilizing Zend_Loader_Autoloader and its subclasses. As long as you follow the PEAR convention, i.e. if you have a class MyLib_Database_Table, then it should be inside the folder MyLib/Database, and the filename should be Table.php. (also make sure that the parent folder of MyLib is on the project include path.

    To autoload simply use new MyLib_Database_Table, and the autoloader will load the class behind the scenes if necessary. Since 1.10 (I think), the autoloader also fully support PHP 5.3 namespaces. I.e:

    // Filepath: lib\MyLib\Database\Table.php
    
    namespace MyLib\Database;
    class Table {
    }
    

    will work with the same folder structure. Code example:

    use MyLib\Database\Table;
    class IndexController extends Zend_Controller_Action
    {
       public function indexAction ()
       {
          $myTable = new Table();
       }
    }
    
  • auto load when I make any new controller

    I'm not quite sure what you mean here. ZF does not have any dependency injection setup per default. But you can instantiate your classes without requiring them first if that's what you mean.