问题
Let's say I have a User_RegistrationController from with I want to extend in another module like so:
class Clinic_RegisterController extends User_RegisterController
but I've PHP error for that:
( ! ) Fatal error: Class 'User_RegisterController' not found /application/modules/clinic/controllers/RegisterController.php on line 4
Is there any way to extend a controller from another module?
回答1:
You could pull your base controller into into a separate library class:
class App_Controller_Action_MyController extends Zend_Controller_Action
{
// your base controller code here
}
Then the controller in module foo
could be empty:
class Foo_MyController extends App_Controller_Action_MyController
{
}
while the controller in module bar
could contain your overrides:
class Bar_MyController extends App_Controller_Action_MyController
{
// override your members/methods
}
Don't forget the add App_
as an autoloadernamespace
.
回答2:
The autoloader is just not set up to find controllers from other modules.
If you want to do what you are doing, you can simply add a require_once
statement above your class definition:
require_once APPLICATION_PATH . '/modules/user/controllers/RegisterController.php';
class Clinic_RegisterController extends User_RegisterController
{
//...
}
回答3:
Why do you need a controller from another module to be extended? This creates a dependency on another module. If there is common code in both controllers use action helpers in the application layer.
来源:https://stackoverflow.com/questions/10075283/share-zend-module-controllers-for-use-in-another-module