douwen1006 2015-09-15 11:32
浏览 48
已采纳

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

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 */ }
}

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

This gives me

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

And

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

  • 写回答

1条回答 默认 最新

  • douyuan1049 2015-09-16 11:36
    关注

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

    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:

    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;
                    },
            ),
        );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要