Extending ZfcUser with Zend Framework 2

2019-04-08 02:30发布

问题:

Hi I am trying to write a user registration form using ZfcUser module for Zend Framwork 2 and would like some advice on best practices when adding more user fields.

So far I have created my own module called "WbxUser" and as outlined in the modules wiki pages I have added a custom field called "userlastname" to ZfcUser's registration form by using the Event Manager in my modules bootstrap function like so.

Code:

//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();
    }
}

But after this I have got a bit lost on where/how to write the code to save the extra data that my new fields are gathering.

  1. Is this the event where I can fire off save routines for the additional fields https://github.com/ZF-Commons/ZfcUser/wiki/How-to-perform-a-custom-action-when-a-new-user-account-is-created

  2. Should I be writing my own WbxUser model that extends ZfcUser\Entity\User.php to add my new fields

I am a bit of a ZF and MVC noob so would be very great-full for a nudge in the write direction.

回答1:

this one seems to be a bit more intuitive

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



回答2:

You can override the module configurations. The most elegant way is to use the zfcuser.global.php.dist in the autoload folder. Copy this file to your appliction autoload folder and change the filename to zfcuser.global.php. In here you're able to change whatever option you can find in here. This option can be used to override the zfcUser entity: 'user_entity_class' => 'ZfcUser\Entity\User'.

Then again, you may find yourself in a situation where you need to change things that cannot be changed in the configuration file. In this case you could create your own user module ( You may want to just clone the entire zfcuser module until you're sure about what files you want to replace.

After cloning the user module (and changed the namespaces accordingly) add your module to application.config.php. Make sure your module is loaded after zfcuser. Or alternatively you could remove zfcuser from the config file and put the following in module.php:

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

Now you can override ZfcUser module configurations. The following snippet could be used to override the template folder and 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',
                            ),
                        ),
                    ),           
                ),
            ),
        ),
    ), 
);

Also you can override ZfcUser services in module.php. ;-)



回答3:

Altering a third party module don't really appeal to me because at times it unsettle a lot of functionality. I always try do something outside the module because if there is an update in the module i easily incorporate into my application without haven to rewrite anything.

To do something like you are trying to do I would create another table in my application and add a foreign key that extend the zfcuser table that way i can relate the information to each zf2user in my application.

It just a suggestion as i am also new in zf2.