2017 © Pedro Peláez
 

library symfony-traits

Helper trait to ease writing APIs in Symfony

image

nebkam/symfony-traits

Helper trait to ease writing APIs in Symfony

  • Tuesday, May 29, 2018
  • by nebkam
  • Repository
  • 1 Watchers
  • 2 Stars
  • 90 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 12 Versions
  • 76 % Grown

The README.md

Latest Stable Version, (*1)

Symfony Traits

Few helper traits for quicker API development in Symfony, (*2)

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:, (*3)

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

The Versions

29/05 2018

dev-master

9999999-dev

Helper trait to ease writing APIs in Symfony

  Sources   Download

MIT

The Requires

 

29/05 2018

1.0.10

1.0.10.0

Helper trait to ease writing APIs in Symfony

  Sources   Download

MIT

The Requires

 

29/05 2018

1.0.9

1.0.9.0

Helper trait to ease writing APIs in Symfony

  Sources   Download

MIT

The Requires

 

02/04 2018

1.0.8

1.0.8.0

Helper trait to ease writing APIs in Symfony

  Sources   Download

MIT

The Requires

 

01/04 2018

1.0.7

1.0.7.0

Helper trait to ease writing APIs in Symfony

  Sources   Download

MIT

The Requires

 

23/03 2018

1.0.6

1.0.6.0

Helper trait to ease writing APIs in Symfony

  Sources   Download

MIT

The Requires

 

23/03 2018

1.0.5

1.0.5.0

Helper trait to ease writing APIs in Symfony

  Sources   Download

MIT

The Requires

 

23/03 2018

1.0.2

1.0.2.0

Helper trait to ease writing APIs in Symfony

  Sources   Download

MIT

The Requires

 

22/03 2018

1.0.1

1.0.1.0

Helper trait to ease writing APIs in Symfony

  Sources   Download

MIT

The Requires

 

22/03 2018

1.0.0

1.0.0.0

Helper trait to ease writing APIs in Symfony

  Sources   Download

MIT

The Requires