Giter VIP home page Giter VIP logo

symfony-traits's Introduction

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 }

symfony-traits's People

Contributors

nebkam avatar shkabo avatar zidar-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

shkabo ybjozee

symfony-traits's Issues

[Documentation] Documentation needed

Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? no
Symfony version 3.4

Is it possible that we have written documentation of this package?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.