2012-11-29 2 views
6

안녕하세요 저는 Zend Framwork 2 용 ZfcUser 모듈을 사용하여 사용자 등록 양식을 작성하려고하고 있으며 더 많은 사용자 필드를 추가 할 때 모범 사례에 대한 조언을 듣고 싶습니다.Zend Framework 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. 이 나의 새로운 필드를 추가 할 ZfcUser \ 엔티티 \ User.php를 확장 난 내 자신의 WbxUser 모델을 작성해야 하는가 나는 추가 필드

  2. https://github.com/ZF-Commons/ZfcUser/wiki/How-to-perform-a-custom-action-when-a-new-user-account-is-created에 대한 루틴 저장 해고 할 수있는 이벤트입니다

저는 ZF와 MVC의 놈입니다. 따라서 쓰기 방향의 찌르기가 아주 큽니다.

+1

이 게시물을 살펴볼 수 있습니다 : http://stackoverflow.com/questions/8783873/zf2-whats-the-best-practice-for-working-with-vendor-modules-form-classes – nXqd

+2

굉장 도움이됩니다. 나는 나의 모듈을 쓸 때마다 균열이 생길 것이고 나는 나를 위해 일한 것을 게시 할 것이다. 다시 한번 감사드립니다. –

+0

감사합니다. 나는 당신을 만나기를 고대하고 있습니다. 나는 똑같은 일을하고있다. :) – nXqd

답변

4

이 한 번에이 기능을 많이 동요 때문에 정말 나에게 호소하지 않는 타사 모듈을 변경하면

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

+0

축하해. 그것은 좋은 블로그 게시물을 읽기 좋은,하지만 그것은 여전히 ​​데이터를 저장하고 양식에 추가 한 컨트롤러를 확장 예제를 가지고 있지 않습니다, 어떤 붙어있어 조금입니다. –

+1

여기에 대한 빠른 가이드가 있습니다. Theo : http://circlical.com/blog/2013/4/1/l5wftnf3p7oks5561bohmb9vkpasp6 아마도 도움이됩니다. – Saeven

0

좀 더 직관적 인 것 같다. 모듈에 업데이트가 있으면 아무 것도 다시 쓰지 않고도 내 응용 프로그램에 쉽게 통합 할 수 있기 때문에 항상 모듈 외부에서 작업을 시도합니다.

내 응용 프로그램에서 다른 테이블을 만들고 zfcuser 테이블을 확장하는 외래 키를 추가하면 내 응용 프로그램의 각 zf2user에 정보를 연결할 수 있습니다.

zf2에서 새롭게 추가 된 것입니다.

1

모듈 구성을 무시할 수 있습니다. 가장 우아한 방법은 autoload 폴더에서 zfcuser.global.php.dist를 사용하는 것입니다. 이 파일을 appliction autoload 폴더에 복사하고 파일 이름을 zfcuser.global.php로 변경하십시오. 여기서 여기서 찾을 수있는 옵션을 변경할 수 있습니다. 이 옵션은 'user_entity_class'=> 'ZfcUser \ Entity \ User'와 같이 zfcUser 엔티티를 덮어 쓸 수 있습니다.

다시 한 번, 구성 파일에서 변경할 수없는 사항을 변경해야하는 상황에 처해있을 수 있습니다. 이 경우 사용자 자신의 사용자 모듈을 만들 수 있습니다 (어떤 파일을 대체 할 것인지 확실하지 않을 때까지 전체 zfcuser 모듈을 복제 할 수 있습니다.)

사용자 모듈을 복제 한 다음 그에 따라 이름 공간을 변경 한 후, . 모듈이 zfcuser 후로드되어 있는지 확인 application.config.php하는 모듈을 추가하거나 또는 당신은 config 파일에서 zfcuser을 제거 할 수 있고 module.php에 다음을 넣어 :.

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

지금 당신이 zfcUser를 오버라이드 (override) 할 수 있습니다 모듈 구성 다음 스 니펫을 사용하여 템플릿 폴더와 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 서비스를 무시할 수 있습니다. ;-)