Php 使用Zend Framework 2扩展ZfcUser
您好,我正在尝试使用Zend Framwork 2的ZfcUser模块编写一份用户注册表,希望在添加更多用户字段时获得一些关于最佳实践的建议 到目前为止,我已经创建了自己的名为“WbxUser”的模块,并且如中所述,通过在我的模块引导函数中使用事件管理器,我在ZfcUser的注册表中添加了一个名为“userlastname”的自定义字段,如下所示 代码:Php 使用Zend Framework 2扩展ZfcUser,php,model-view-controller,zend-framework2,Php,Model View Controller,Zend Framework2,您好,我正在尝试使用Zend Framwork 2的ZfcUser模块编写一份用户注册表,希望在添加更多用户字段时获得一些关于最佳实践的建议 到目前为止,我已经创建了自己的名为“WbxUser”的模块,并且如中所述,通过在我的模块引导函数中使用事件管理器,我在ZfcUser的注册表中添加了一个名为“userlastname”的自定义字段,如下所示 代码: //WbxUser.Module.php namespace WbxUser; use Zend\Mvc\MvcEvent; c
//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();
}
}
但是在这之后,我有点迷失了在哪里/如何编写代码来保存我的新字段正在收集的额外数据
我是一个有点ZF和MVC的noob,所以在写的方向上有一个很好的推动 这个似乎更直观一些
修改第三方模块并不真正吸引我,因为它有时会扰乱很多功能。我总是尝试在模块之外做一些事情,因为如果模块中有更新,我可以轻松地将其合并到我的应用程序中,而无需重写任何内容 要执行与您尝试执行的操作类似的操作,我将在应用程序中创建另一个表,并添加一个外键,以扩展zfcuser表,从而将信息关联到应用程序中的每个ZF2用户
这只是一个建议,因为我也是zf2的新手。您可以覆盖模块配置。最优雅的方法是使用autoload文件夹中的zfcuser.global.php.dist。将此文件复制到appliction autoload文件夹,并将文件名更改为zfcuser.global.php。在这里,你可以改变任何你可以在这里找到的选项。此选项可用于覆盖zfcUser实体:“用户\实体\类”=>“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',
),
),
),
),
),
),
),
);
你可以看看这篇帖子:太棒了,真的很有帮助。我将尝试编写我的模块,我将发布对我有用的内容。再次感谢。谢谢,我期待着看到你的。我也在做同样的事情:)干杯,谢谢。这是一篇很好、很容易阅读的博客文章,但是它仍然没有任何例子可以在你添加到表单后保存数据和扩展控制器,这是我一直坚持的一点。我这里有一个快速指南西奥:也许会有帮助