首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用2扩展ZfcUser

用2扩展ZfcUser
EN

Stack Overflow用户
提问于 2012-11-29 16:43:10
回答 3查看 6.3K关注 0票数 6

嗨,我正在尝试使用Zend 2的ZfcUser模块编写一个用户注册表单,并希望在添加更多用户字段时提供一些关于最佳实践的建议。

到目前为止,我已经创建了自己的模块"WbxUser“,并且正如在modules wiki pages中所概述的那样,我在ZfcUser的注册表单中添加了一个名为"userlastname”的自定义字段,方法是在我的模块引导函数中使用事件管理器,如下所示。

代码:

代码语言:javascript
复制
//WbxUser.Module.php   

namespace WbxUser;

use Zend\Mvc\MvcEvent;


class Module {
    public function onBootstrap(MvcEvent $e){
        $events = $e->getApplication()->getEventManager()->getSharedManager();
        $events->attach('ZfcUser\Form\Register','init', function($e) {
            $form = $e->getTarget();
            $form->add(array(
                    'name' => 'userlastname',
                    'attributes' => array(
                            'type'  => 'text',
                    ),
                    'options' => array(
                            'label' => 'Last Name',
                    ),
            ));
            // Do what you please with the form instance ($form)
        });

        $events->attach('ZfcUser\Form\RegisterFilter','init', function($e) {
            $filter = $e->getTarget();

            $filter->add(array(
                'name'       => 'userlastname',
                'required'   => true,
                'filters'  => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name'    => 'StringLength',
                        'options' => array(
                            'min' => 3,
                            'max' => 255,
                        ),
                    ),
                ),
           ));
        });
    }

    public function getConfig(){
        return array();
    }

    public function getAutoloaderConfig(){
        return array();
    }
}

但在这之后,我对如何编写代码以保存我的新字段所收集的额外数据感到有点迷茫。

  1. 这是我可以为附加字段https://github.com/ZF-Commons/ZfcUser/wiki/How-to-perform-a-custom-action-when-a-new-user-account-is-created启动保存例程的事件吗?
  2. 我是否应该编写自己的WbxUser模型来扩展ZfcUser\Entity\User.php以添加我的新字段

我有点ZF和MVC noob,所以在写作方向上我会非常满意。

EN

回答 3

Stack Overflow用户

发布于 2012-12-05 23:50:01

这个似乎更直观一些。

http://juriansluiman.nl/en/article/117/use-3rd-party-modules-in-zend-framework-2

票数 4
EN

Stack Overflow用户

发布于 2013-09-07 14:46:50

您可以覆盖模块配置。最优雅的方法是在autoload文件夹中使用zfcuser.global.php.dist。将此文件复制到应用程序自动加载文件夹,并将文件名更改为zfcuser.global.php。在这里,你可以改变你能在这里找到的任何选项。此选项可用于覆盖zfcUser实体:'user_entity_class‘=> 'ZfcUser\Entity\User’。

然后,您可能会发现自己处于需要更改配置文件中无法更改的内容的情况下。在这种情况下,您可以创建自己的用户模块(您可能只想克隆整个zfcuser模块,直到确定要替换哪些文件为止。

克隆用户模块(并相应地更改名称空间)之后,将模块添加到application.config.php中。确保您的模块在zfcuser之后加载。或者,您也可以从配置文件中删除zfcuser,并将以下内容放在module.php中:

代码语言:javascript
复制
public function init($moduleManager)
{
    $moduleManager->loadModule('ZfcUser');
}

现在您可以重写ZfcUser模块配置。下面的片段可用于覆盖模板文件夹和UserController。

代码语言:javascript
复制
<?php

// Note most routes are managed in zfcUser config file.
// All settings here either overrides or extend that functionality.

return array(
    'view_manager' => array(
        'template_path_stack' => array(
            // 'zfcuser' => __DIR__ . '/../view',  Override template path           
        ),
        'template_map' => array(
            // You may want to use templates from the original module without having to copy - paste all of them.  
        ),
    ),

    'controllers' => array(
        'invokables' => array(
            // 'zfcuser' => 'YourModule\Controller\IndexController', // Override ZfcUser controller.
        ),
    ),

    'router' => array(
        'routes' => array(
            'zfcuser' => array(
                'type' => 'Literal',
                'priority' => 2500,
                'options' => array(
                    'route' => '/user',
                    'defaults' => array(

                        // Use original ZfcUser controller. 
                        // It may be a good idea to use original code where possible

                        'controller' => 'ZfcUser\Controller\UserController',
                        'action'     => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(

                    'authenticate' => array(
                        'type' => 'Literal',
                        'options' => array(
                            'route' => '/authenticate',
                            'defaults' => array(

                                // Invoke YourModule\Controller\IndexController

                                'controller' => 'zfcuser',
                                'action'     => 'authenticate',
                            ),
                        ),
                    ),           
                ),
            ),
        ),
    ), 
);

此外,还可以在ZfcUser中覆盖module.php服务。;-)

票数 1
EN

Stack Overflow用户

发布于 2013-07-03 09:47:07

修改第三方模块对我并不是很有吸引力,因为有时它会扰乱很多功能。我总是尝试在模块之外做一些事情,因为如果模块中有更新,我很容易将其合并到我的应用程序中,而无需重写任何内容。

要做您想做的事情,我将在我的应用程序中创建另一个表,并添加一个外键来扩展zfcuser表,这样我就可以将信息关联到应用程序中的每个zf2user。

这只是一个建议,因为我也是新的zf2。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13630526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档