Extending the IndexController with a BaseController in Zend

风流意气都作罢 提交于 2019-12-03 06:10:38
markus

Autoloader

Setup the autoloader and register your library which should be besides the Zend library with the autoloader like so (in your bootstrap.php after setting the include path):

//AutoLoad loads classes automatically if they are used
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('Mylibrary_');

Zend naming conventions

Then you should rename your BaseController as follows

/Zend (folder)
/Mylibrary (folder)
    /Controller (folder)
        Action.php <-- this is your basecontroller file

which contains:

class Mylibrary_Controller_Action extends Zend_Controller_Action
{
}

and your normal controllers in the controller folder:

class IndexController extends Mylibrary_Controller_Action
{
}

so basically when you want to extend the framework you keep a parallel structure in your own library.

I would separate it into your own library, i.e. create the file library/YourApp/Controller/Action.php, and consequently name it YourApp_Controller_Action extends Zend_Controller_Action. From there you could place controllers where they should be and let them extend YourApp_Controller_Action in favor of Zend_Controller_Action.

To find the file you should rely on the autoloader to look not just inside of library/Zend, but also in library/YourApp. I.e. look for the set_include_path in your bootstrap.

With this technique you should keep in mind that your custom "basecontroller" might get bloated with methods that not all of your controllers needs to inherit.

Even more quicker solution (and conceptually more correct) is NOT to create base controllers at all:)

You have common action? Use action helpers. You have some functionality that must be autorun? Use controller plugins.

By design ZF controllers were made as flexible as possible, and limiting yourself by inheritance (and coupling it brings) is just not the best possible strategy.

the quick solution that does not take advantage of the autoloader functionality is to require_once '/path/to/BaseController.php' in the index-controller file.

If you have set-up autocontroller, then it can not find it, so you should consider checking what's wrong. Try the previous approach and inform on results.

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