我有一个名为“olo”的模块,它处理我们所有的在线订购。
现在我制作了一个名为“olosec”的新模块,因为我希望制作一个稍微改变流程的不同版本,并对一些 Controller 进行一些其他更改。
我可以用“olo”中的 Controller 扩展“olosec”中的 Controller 吗?
到目前为止,我已经尝试过了
class Olosec_CartController extends Olo_CartController
这会抛出类似的错误
Warning: include_once(Olo/CartController.php): failed to open stream:
No such file or directory in /httpdocs/library/Zend/Loader.php on line 146 Warning:
include_once(): Failed opening 'Olo/CartController.php' for inclusion.
bla bla bla (include path) bla bla bla
我的目录结构是这样的(感谢 tree\F\A
和 EditPlus++ )
+---application
| +---views
| | +---scripts
| | +---layouts
| | | +---default
| | | +---admin
| | +---languages
| | +---helpers
| +---modules
| | +---admin
| | +---mobile
| | +---olo
| | | +---controllers
| | | IndexController.php
| | | MenuController.php
| | | CartController.php
| | | OrderlistController.php
| | | |
| | | +---models
| | | \---views
| | | +---helpers
| | | \---scripts
| | | +---index
| | | +---menu
| | | +---cart
| | | \---orderlist
| | \---olosec
| | +---controllers
| | | IndexController.php
| | | MenuController.php
| | | CartController.php
| | | OrderlistController.php
| | |
| | +---models
| | \---views
| | +---helpers
| | \---scripts
| | +---index
| | +---menu
| | +---cart
| | \---orderlist
| +---models
| +---controllers
| \---configs
+---library
+---public
| +---cli
| \---default
+---tests
\---data
更新
我已经使用了这个有效的“讨厌的”技巧
require_once( APPLICATION_PATH . '/modules/olo/controllers/CartController.php');
更新@Rakesh
我的 Bootstrap 中有这个..
function _initAutoloader() {
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);
return $autoloader;
}
在我的 application.ini 中
autoloadernamespaces.0 = "Zend"
autoloadernamespaces.1 = "My"
autoloadernamespaces.2 = "Something"
最佳答案
为什么不为公共(public)类设置自定义库文件夹
application/
library/ < for common classes
如果您不仅在一个 Controller 中而且在项目的许多地方使用某些类,这是一种很好的方法。
您只需将这个新的 application/library/
文件夹添加到您的 boostrap 文件中的 include path
。
另一种方法是使用 Action 助手。但正如我所描述的,公共(public)类文件夹应该是一个很好的解决方案。但是我发现了一些有趣的资源,其中大部分是关于跨模块编码的,但无论如何它们可能对您有所帮助 http://zend-framework-community.634137.n4.nabble.com/Code-re-use-across-modules-td668554.html 和 How to Use Same Models in Different Modules in Zend Framework?
让我描述另一种方法
class BasicController extends Zend_Controller_Action
{
public static $variable = '';
public function init()
{
self::$variable = 'assign value';
}
}
class HomeController extends BasicController
{
public function indexAction()
{
$bioVar = parrent::$variable;
}
}
这比简单地扩展 Controller 要好,因为它们代表 Action 并且每个 Action 都有对应的 View 脚本。但是,您所有的类都应该在自动加载器中注册。
关于php - 从另一个模块中的 Controller 扩展 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5894481/