【问题标题】:Extending ZfcUser with Zend Framework 2使用 Zend Framework 2 扩展 ZfcUser
【发布时间】:2012-11-29 16:43:10
【问题描述】:

您好,我正在尝试使用 Zend Framwork 2 的 ZfcUser 模块编写用户注册表单,并希望在添加更多用户字段时获得有关最佳实践的一些建议。

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

代码:

//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 菜鸟,所以对于写入方向的轻推,我会非常满意。

【问题讨论】:

  • 你可以看看这个帖子:stackoverflow.com/questions/8783873/…
  • 真棒,确实有帮助。我将在编写我的模块时有所突破,我将发布对我有用的内容。再次感谢。
  • 谢谢,我期待看到你的。我正在做同样的事情:)

标签: php model-view-controller zend-framework2


【解决方案1】:

【讨论】:

  • 干杯谢谢。这是一篇易于阅读的好博文,但它仍然没有任何保存数据和扩展控制器的示例,一旦您添加到表单,这是我坚持的一点。
  • 我在这里有一个快速指南 Theo:circlical.com/blog/2013/4/1/l5wftnf3p7oks5561bohmb9vkpasp6 也许有帮助
【解决方案2】:

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

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

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

public function init($moduleManager)
{
    $moduleManager->loadModule('ZfcUser');
}

现在您可以覆盖 ZfcUser 模块配置。 以下 sn-p 可用于覆盖模板文件夹和 UserController。

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

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

【讨论】:

    【解决方案3】:

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

    要执行您尝试执行的操作,我将在我的应用程序中创建另一个表并添加一个扩展 zfcuser 表的外键,这样我就可以将信息与我的应用程序中的每个 zf2user 关联起来。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-10
      相关资源
      最近更新 更多