定制 nebkam/symfony-traits 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nebkam/symfony-traits

最新稳定版本:4.1.2

Composer 安装命令:

composer require nebkam/symfony-traits

包简介

Helper trait to ease writing APIs in Symfony

README 文档

README

Latest Stable Version

Symfony Traits

Few helper traits for quicker API development in Symfony

FormTrait

handleJSONForm

  • controller helper method for JSON data sent in POST, PUT or PATCH request content
  • generally speaking, sending JSON content is more flexible than forms, for CRUD with JS frameworks.
public function create(Request $request)
	{
	$entity = new Entity();
	$this->handleJSONForm($request, $entity, EntityType::class, $options = [], $clearMissingFields = true);
	// persist and flush $entity
public function edit(Request $request,Entity $entity)
	{
	$this->handleJSONForm($request, $entity, EntityType::class, $options = [], $clearMissingFields = true);
	// flush entity

handleForm

  • controller helper method for traditional form data in GET or POST
  • I advice using traditional form data only when JSON is out of place (i.e. GET params)
public function example(Request $request)
	{
	$domain = new Domain();
	if ($request->query->count() > 0)
		{
		$this->handleForm($request, $params, DomainType::class, $options = [], $clearMissingFields = true);
		}
	
	// do something with $domain

handleUpload

  • controller helper method to validate a single file upload
  • a lightweight alternative to a File constraint, when you need just one file, not the whole form
public function uploadImage(Request $request)
	{
	$file = $this->handleUpload($request, 'image');
	
	// do something with $file
	}

ValidationExceptionListener

Since all handle* methods in this trait throw a Nebkam\SymfonyTraits\ValidationException, you have to catch it, either via try {..} catch in the controller or via global exception listener. To ease this, this package includes a sample exception listener, which returns validation errors in JSON. You just have to register it as a service:

Nebkam\SymfonyTraits\EventListener\ValidationExceptionListener:
    tags:
        - { name: kernel.event_listener, event: kernel.exception }

统计信息

  • 总下载量: 6.19k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 3
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-22