2016-02-03 83 views
0

我試圖在寄存器中的zfcuser模塊中添加新的寄存器字段。我有問題BCS新領域中register.phtmlZfcuser添加寄存器字段

不會呈現我該怎麼辦:

首先我在自定義模塊創建新User實體和擴展\ZfcUser\Entity\User,並添加新的特性protected $first_name並把getter方法。

其次,我在config中更改user_entity_class' => 'MyModule\Entity\User

三,我創建自定義窗體類,其中我擴展\ZfcUser\Form\Register其中我創建兩個方法__constructor($name,RegistrationOptionsInterface $options),第二init()。這看起來是這樣的:

// Module/src/Mymod/Form 

class ClientRegisterForm extends \ZfcUser\Form\Register 
{ 
    public function __construct($name, RegistrationOptionsInterface $options) 
    { 
     parent::__construct($name, $options); 
    } 

    /** 
    * {@inheritDoc} 
    */ 
    public function init(){ 
     $this->add(array(
      'name' => 'first_name', 
      'options' => array(
       'label' => 'First name', 
      ), 
      'attributes' => array(
       'type' => 'text' 
      ), 
     )); 
} 

和我模塊註冊此類似sercice:

public function getServiceConfig() 
{ 
    return array(
     'factories' => array(
      'clientRegisterForm' => function($sm) { 
       $clientRegisterForm = new ClientRegisterForm(null, array()); 

       return $clientRegisterForm; 
      } 
     ) 
    ); 
} 

所以問題是BCS zfcuser不知道任何關於新領域。循環列表只是默認字段。如何以這種方式通知zfcuser模塊關於新領域?

register.phtml

<?php foreach ($form as $element): ?> 
<?php echo $this->formInput($element) . $this->formElementErrors($element) ?> 
<?php endforech; ?> 

回答

0

通常你會先修改module.config.php,使其正確反映您zfcuser_entity。

return array(
    'doctrine' => array(
     'driver' => array(
      // overriding zfc-user-doctrine-orm's config 
      'zfcuser_entity' => array(
       'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver', 
       'paths' => __DIR__ . '/../src/FooUser/Entity', 
      ), 

      'orm_default' => array(
       'drivers' => array(
        'FooUser\Entity' => 'zfcuser_entity', 
       ), 
      ), 
     ), 
    ), 

    'zfcuser' => array(
     // telling ZfcUser to use our own class 
     'user_entity_class'  => 'FooUser\Entity\User', 
     // telling ZfcUserDoctrineORM to skip the entities it defines 
     'enable_default_entities' => false, 
    ), 
); 

然後在你的引導你需要聽重視ZfcUser \表格\ RegisterFilter,ZfcUser \表格\註冊(包括初始化)修改窗體。

最後,同樣在您的引導程序中,您將附加到'zfcuser_user_service'事件管理器上的'register'事件。

namespace FooUser; 

use Zend\Mvc\MvcEvent; 

class Module { 

    public function getConfig() 
    { 
     return include __DIR__ . '/config/module.config.php'; 

    } 

    public function getAutoloaderConfig() 
    { 
     return array(
      'Zend\Loader\StandardAutoloader' => array(
       'namespaces' => array(
        __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, 
       ), 
      ), 
     ); 
    } 

    public function onBootstrap(MVCEvent $e) 
    { 
     $eventManager = $e->getApplication()->getEventManager(); 
     $em   = $eventManager->getSharedManager(); 
     $em->attach(
      'ZfcUser\Form\RegisterFilter', 
      'init', 
      function($e) 
      { 
       $filter = $e->getTarget(); 
       // do your form filtering here    
      } 
     ); 

     // custom form fields 

     $em->attach(
      'ZfcUser\Form\Register', 
      'init', 
      function($e) 
      { 
       /* @var $form \ZfcUser\Form\Register */ 
       $form = $e->getTarget(); 
       $form->add(
        array(
         'name' => 'username', 
         'options' => array(
          'label' => 'Username', 
         ), 
         'attributes' => array(
          'type' => 'text', 
         ), 
        ) 
       ); 

       $form->add(
        array(
         'name' => 'favorite_ice_cream', 
         'options' => array(
          'label' => 'Favorite Ice Cream', 
         ), 
         'attributes' => array(
          'type' => 'text', 
         ), 
        ) 
       ); 
      } 
     ); 

     // here's the storage bit 

     $zfcServiceEvents = $e->getApplication()->getServiceManager()->get('zfcuser_user_service')->getEventManager(); 

     $zfcServiceEvents->attach('register', function($e) { 
      $form = $e->getParam('form'); 
      $user = $e->getParam('user'); 
      /* @var $user \FooUser\Entity\User */ 
      $user->setUsername( $form->get('username')->getValue()); 
      $user->setFavoriteIceCream($form->get('favorite_ice_cream')->getValue()); 
     }); 

     // you can even do stuff after it stores   
     $zfcServiceEvents->attach('register.post', function($e) { 
      /*$user = $e->getParam('user');*/ 
     }); 
    } 
} 

我已經得到了通過細節這裏走一個完整的帖子: http://circlical.com/blog/2013/4/1/l5wftnf3p7oks5561bohmb9vkpasp6

現在,我只是推出自己的用戶實體,驗證服務結合我自己的登錄,註冊形式和等等。最初我喜歡ZfcUser和BjyAuthorize的組合 - 但BjyAuthorize +自定義更好!

祝你好運。