oh/form-error-log-bundle
Composer 安装命令:
composer require oh/form-error-log-bundle
包简介
Log form errors
README 文档
README
Log form errors.
Functional testing your forms is all well and good, but how do you know your users are using it correctly? This plugin can log the errors your users are making so that you can spot any usability problems.
WARNING: If the error is on the whole form this bundle tries to json_encode your bound entity. If it can't be json_encoded it will try to serialize before finally logging the whole _POST request for the form. THIS IS A SECURITY RISK IF USED ON FORMS CONTAINING SENSITIVE DATA, LIKE PASSWORDS OR CREDIT CARD INFORMATION. If this is the case, you should implement the Serializeable or JsonSerializable (PHP 5.4) interfaces on your bound objects to block out the sensitive data.
Installation
This bundle is alpha stability due to the lack of testing on different form types. Your composer.json needs to reflect that by setting the minimum-stability to "alpha" or "dev"
"minimum-stability": "alpha"
Install this bundle as usual by adding to composer.json:
"oh/form-error-log-bundle": "dev-master"
Register the bundle in app/AppKernel.php:
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new Oh\FormErrorLogBundle\OhFormErrorLogBundle(),
);
}
Set up
There are 2 logging methods provided. One uses your normal logger (Monolog) and the other logs into a database
Method 1: Monolog
You will need to create a new channel in your monolog settings called 'formerror'
#app/config/config_prod.yml
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
nested:
type: stream
path: %kernel.logs_dir%/%kernel.environment%.log
level: debug
formerror:
type: stream
path: %kernel.logs_dir%/form-error-%kernel.environment%.log
channels: formerror
Method 2: Database
This uses Doctrine. You should create your own Entity which implements FormErrorLogEntityInterface
<?php
namespace Your\Bundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Oh\FormErrorLogBundle\Entity\FormErrorLogEntityInterface;
/**
* @ORM\Table(name="form_error_log")
* @ORM\Entity
*/
class FormErrorLog implements FormErrorLogEntityInterface
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(name="form_name", type="string", length=255)
* @var type
*/
private $form_name;
/**
* @var string $field
*
* @ORM\Column(name="field", type="string", length=255)
*/
private $field;
/**
* @var string $error
*
* @ORM\Column(name="error", type="string", length=2000)
*/
private $error;
/**
* @var string $error
*
* @ORM\Column(name="value", type="string", length=2000)
*/
private $value;
/**
* @var string $uri
*
* @ORM\Column(type="string", length=512)
*/
private $uri;
public function getFormName()
{
return $this->form_name;
}
public function setFormName($formName)
{
$this->form_name = $formName;
}
public function getField()
{
return $this->field;
}
public function setField($field)
{
$this->field = $field;
}
public function getError()
{
return $this->error;
}
public function setError($error)
{
$this->error = $error;
}
public function getValue()
{
return $this->value;
}
public function setValue($value)
{
$this->value = $value;
}
public function setUri($uri)
{
$this->uri = $uri;
return $this;
}
public function getUri()
{
return $this->uri;
}
}
You can create your own methods to store the date (I use Gedmo Timestampable)
In your parameters.yml you can set the class to your entity
#app/config/parameters.yml
oh_form_error_log.db.entity.class: Your\Bundle\Entity\FormErrorLog
Your Form
Insert the listener into your form class:
#YourBundle/Form/YourEntityType.php
<?php
namespace Your\Bundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class YourEntityType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
if($options['logger']) {
$builder->addEventSubscriber($options['logger']);
}
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Your\Bundle\Entity\YourEntity',
'logger'=>false
));
}
public function getName()
{
return 'your_bundle_yourentity';
}
}
And in your controller
<?php
namespace Your\Bundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Your\Bundle\Entity\YourEntityType;
class YourController extends Controller
{
public function createAction()
{
$form = $this->createForm(new YourEntityType(), $entity, array(
'logger'=>$this->get('oh_form_error_log.listener'))
// or for the database version
//'logger'=>$this->get('oh_form_error_log.listener.db'))
);
if ($form->isValid()) {
// do stuff
}
return array(
'form' => $form->createView(),
);
}
}
Todo
- Tests
- Support for Symfony\Component\Serializer\Normalizer
- Test with different FormTypes (like FileType)
Credits
- Ollie Harridge (ollietb) as the author.
oh/form-error-log-bundle 适用场景与选型建议
oh/form-error-log-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.61k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2012 年 12 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Symfony2」 「form」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 oh/form-error-log-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 oh/form-error-log-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 oh/form-error-log-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Diese Contao 4 Erweiterung stellt Google reCAPTCHA V2 in Form eines neuen Formularfeldes im Formulargenerator bereit. This extension provides Google reCAPTCHA V2 in the form of a new form field in the form generator of Contao Open Source CMS.
A Laravel Filament Forms slug field.
Symfony2 Barcode Generator Bundle with Twig function extension
A jQuery augmented PHP library for creating and validating HTML forms
Symfony bundle to connect AWS sns and sqs to create offline queue processing
Integrate the doctrine-translatable extension in Symfony2
统计信息
- 总下载量: 7.61k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2012-12-08