2012-11-29 51 views
6

您好我正在嘗試使用ZfcUser模塊爲Zend Framwork 2編寫用戶註冊表單,並希望在添加更多用戶字段時提供有關最佳實踐的一些建議。使用Zend Framework擴展ZfcUser 2

到目前爲止,我已經創建了我所謂的「WbxUser」自己的模塊,並作爲modules wiki pages概述我已經通過事件管理器在我的模塊自舉功能,像這樣增加了一個叫做「USERLASTNAME」到ZfcUser的登記表的自定義字段。

代碼:

//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. 這是一個擴展ZfcUser \實體\ user.php的加我新的領域時,我可以斷火保存程序的附加字段https://github.com/ZF-Commons/ZfcUser/wiki/How-to-perform-a-custom-action-when-a-new-user-account-is-created

  2. 我應該寫我自己WbxUser模型

我是一個ZF和MVC noob的一點,所以在寫方向上微調會非常棒。

+1

你可以看看這篇文章:http://stackoverflow.com/questions/8783873/zf2-whats-the-best-practice-for-working-with-vendor-modules-form-classes – nXqd

+2

真棒這確實有幫助。我會在寫我的模塊時有一個漏洞,我會發布爲我工作的東西。再次感謝。 –

+0

謝謝,我很期待看到你的。我正在做同樣的事情:) – nXqd

回答

4
+0

乾杯謝謝你。這是一個很容易閱讀的博客文章,但它仍然沒有任何保存數據和擴展控制器的例子,一旦你添加到表單中,這是我堅持的一點。 –

+1

我在這裏有一個快速指南Theo:http://circlical.com/blog/2013/4/1/l5wftnf3p7oks5561bohmb9vkpasp6也許它有幫助 – Saeven

0

改變第三方模塊真的不吸引我,因爲有時它動搖了很多功能。我總是嘗試在模塊之外做某件事情,因爲如果模塊中有更新,我很容易將其納入我的應用程序,而無需重寫任何東西。

要做類似於您正在嘗試的操作,我會在應用程序中創建另一個表並添加一個擴展zfcuser表的外鍵,這種方式可以將信息與我的應用程序中的每個zf2user相關聯。

這只是一個建議,因爲我也是zf2中的新手。

1

您可以覆蓋模塊配置。最優雅的方法是使用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模塊配置 以下片段可用於覆蓋模板文件夾和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服務。 ;-)