在自定义模块中扩展ZfcUser的UserController [英] Extending ZfcUser's UserController in custom module

查看:125
本文介绍了在自定义模块中扩展ZfcUser的UserController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my custom Module I have a custom UserController that extends the ZfcUser vendor's UserController in order to customize the indexAction and registerAction.

namesapce MyModule;

class UserController extends ZfcUser\Controller\UserController
{
    public function indexAction() { /* my code */ }

    public function registerAction() { /* my code */ }
}

我在自定义模块的module.config.php中添加了以下内容:

I add the following in my custom Module's 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中调用在C:\ xampp \ htdocs \ my-module \ vendor \ zf-commons \ zfc-user \ src \ ZfcUser \ Controller \ UserController.php中定义

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 消息:您必须提供一个可调用的redirectCallback

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. This needs to be injected.

要注册您的自定义控制器,您将必须创建一个自定义工厂并注入此依赖项:

To register your custom controller you will have to make a custom factory and inject this dependency:

'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工厂

You could also keep the old route definition and by using the same controller name by only overwriting the original zfcuser factory from the ZfcUser module.php controller config:

您只需要在ZfcUser模块之后加载模块,并将此代码添加到您的module.php:

You just have to load your module after the ZfcUser module and add this code in your 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;
                },
        ),
    );
}

这篇关于在自定义模块中扩展ZfcUser的UserController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆