在自定义模块中扩展 ZfcUser 的 UserController

Extending ZfcUser's UserController in custom module

在我的自定义模块中,我有一个自定义 UserController,它扩展了 ZfcUser 供应商的 UserController,以便自定义 indexActionregisterAction

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',
                        ),
                    ),
                ),
            ),
        ),

这给了我

Warning: Missing argument 1 for ZfcUser\Controller\UserController::__construct(), called in C:\xampp\htdocs\my-module\vendor\zendframework\zend-servicemanager\src\AbstractPluginManager.php on line 207 and defined in C:\xampp\htdocs\my-module\vendor\zf-commons\zfc-user\src\ZfcUser\Controller\UserController.php on line 66

InvalidArgumentException C:\xampp\htdocs\my-module\vendor\zf-commons\zfc-user\src\ZfcUser\Controller\UserController.php:69 Message: You must supply a callable redirectCallback

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;
                },
        ),
    );
}