在我的自定义模块中,我有一个自定义UserController
,可以扩展ZfcUser
供应商的UserController
,以便自定义indexAction
和registerAction
namesapce MyModule;
class UserController extends ZfcUser\Controller\UserController
{
public function indexAction() { /* my code */ }
public function registerAction() { /* my code */ }
}
我在自定义模块module.config.php
中添加以下内容:
// some more config
'controllers' => array(
'invokables' => array(
'MyModule\Controller\User' => 'MyModule\Controller\UserController',
),
),
'router' => array(
'routes' => array(
/**
* Overriding zfcuser route
* https://juriansluiman.nl/article/117/use-3rd-party-modules-in-zend-framework-2
*/
'zfcuser' => array(
'options' => array(
// to override the slug
// 'route' => '/profile',
'defaults' => array(
'controller' => 'MyModule\Controller\User',
'action' => 'index',
),
),
'child_routes' => array(
'register' => array(
'options' => array(
'defaults' => array(
'controller' => 'MyModule\Controller\User',
'action' => 'register',
),
),
),
),
),
这给了我
警告:缺少ZfcUser \ Controller \ UserController :: __ construct()的参数1,在第207行的C:\ xampp \ htdocs \ my-module \ vendor \ zendframework \ zend-servicemanager \ src \ AbstractPluginManager.php中调用在第66行的C:\ xampp \ htdocs \ my-module \ vendor \ zf-commons \ zfc-user \ src \ ZfcUser \ Controller \ UserController.php中定义
和
InvalidArgumentException C:\ XAMPP \ htdocs中\我的模块\供应商\ ZF-公共\ ZFC-用户的\ src \ ZfcUser \控制器\ UserController.php:69 消息:您必须提供可调用的redirectCallback
答案 0 :(得分:1)
The zfc UserController
has a redirect callback dependency in the constructor。这需要注入。
要注册自定义控制器,您必须创建自定义工厂并注入此依赖项:
'controllers' => array(
'invokables' => array(
),
'factories' => array(
'MyModule\Controller\User' => function($controllerManager) {
/* @var ControllerManager $controllerManager*/
$serviceManager = $controllerManager->getServiceLocator();
/* @var RedirectCallback $redirectCallback */
$redirectCallback = $serviceManager->get('zfcuser_redirect_callback');
/* @var UserController $controller */
$controller = new UserController($redirectCallback);
return $controller;
},
)
)
您还可以保留旧路线定义,并使用相同的控制器名称,只覆盖原始zfcuser
工厂from the ZfcUser module.php
controller config:
您只需在ZfcUser模块之后加载模块,并在module.php
中添加此代码:
public function getControllerConfig()
{
return array(
'factories' => array(
'zfcuser' => function($controllerManager) {
/* @var ControllerManager $controllerManager*/
$serviceManager = $controllerManager->getServiceLocator();
/* @var RedirectCallback $redirectCallback */
$redirectCallback = $serviceManager->get('zfcuser_redirect_callback');
/* @var \MyModule\Controller\UserController $controller */
$controller = new \MyModule\Controller\UserController ($redirectCallback);
return $controller;
},
),
);
}