Api: How to use transformers? (question)

Created on 3 Oct 2016  路  4Comments  路  Source: dingo/api

Hello!

I have read wiki and finally don't understand how to use transformers.
Do I need to put lines into Controller or in separate file somewhere?

This code works:

public function index()
{
    return Employee::all();
}

This code not:

public function index()
{
    $employees = Employee::all();
    return $this->response->collection($employees, new EmployeeTransformer);
}

Error:

Class 'App\\Http\\Controllers\\EmployeeTransformer' not found

Sorry, but I have not found an example how to use it.

Env:
Laravel 5.3
Latest Dingo/API

Most helpful comment

I added one line to composer.json:

"autoload": {
        "classmap": [
            "database",
            "app/Transformers"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },

and composer dump-autoload in console...

Now my resource show me this error:

Argument 1 passed to EmployeeTransformer::transform() must be an instance of Employee, instance 
of App\\Employee given, called in 
C:\\OpenServer\\domains\\drugs.lar\\laravel\\vendor\\league\\fractal\\src\\Scope.php on line 365 
and defined

UPDATE (after 5 minutes)

Line use App\Employee; in EmployeeTransformer.php solved my problem.

Is this correct way to use Transformers?

UPDATE (after 30 minutes)

Finally I did it without editing composer.json.

app\Http\Controllers\EmployeeController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;

use Dingo\Api\Routing\Helpers;
use App\Employee;
use App\Transformers\EmployeeTransformer;

class EmployeeController extends Controller
{
    use Helpers;
    //
    //
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $employees = Employee::all();
        return $this->response->collection($employees, new EmployeeTransformer);
    }

}

app\Transformers\EmployeeTransformer.php:

<?php

namespace App\Transformers;

use League\Fractal\TransformerAbstract;
use App\Employee;

class EmployeeTransformer extends TransformerAbstract
{

    /**
     * Turn this item object into a generic array
     *
     * @return array
     */
    public function transform(Employee $employee)
    {
        return [
            'id'            => (int) $employee->id,
            'firstname'     => $employee->firstname,
            'lastname'      => $employee->lastname,
            'middlename'    => $employee->middlename
        ];
    }

}

I think wiki need update with examples :wink:

All 4 comments

You can follow this: https://github.com/dingo/api/wiki/Responses

You can define a transforme class like this: http://fractal.thephpleague.com/transformers/

A Transformer is a class that tells what ever you want to a expose for a resource.

About resources: http://restful-api-design.readthedocs.io/en/latest/resources.html

Ok. I found this issue with some instructions:
https://github.com/dingo/api/issues/341

Step by step:
1_ Created app\Transformers folder.
2_ Created app\Transformers\EmployeeTransformer.php file with following:

<?php

use League\Fractal\TransformerAbstract;

class EmployeeTransformer extends TransformerAbstract
{

    /**
     * Turn this item object into a generic array
     *
     * @return array
     */
    public function transform(Employee $employee)
    {
        return [
            'id'            => (int) $employee->id,
            'firstname'     => $employee->firstname,
            'lastname'      => $employee->lastname,
            'middlename'    => $employee->middlename
        ];
    }

}

3_ My app\Http\Controllers\EmployeeContoller.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;

use Dingo\Api\Routing\Helpers;
use App\Employee;
use EmployeeTransformer;

class EmployeeController extends Controller
{
    use Helpers;
    //
    //
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $employees = Employee::all();
        return $this->response->collection($employees, new EmployeeTransformer);

        //return Employee::all();
    }

}

4_ My model in app\Employee.php:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    protected $table = 'employees';
}

And finally still an error:

Class 'EmployeeTransformer' not found

What I'm doing wrong?
I think it's problem with autoloader...

I added one line to composer.json:

"autoload": {
        "classmap": [
            "database",
            "app/Transformers"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },

and composer dump-autoload in console...

Now my resource show me this error:

Argument 1 passed to EmployeeTransformer::transform() must be an instance of Employee, instance 
of App\\Employee given, called in 
C:\\OpenServer\\domains\\drugs.lar\\laravel\\vendor\\league\\fractal\\src\\Scope.php on line 365 
and defined

UPDATE (after 5 minutes)

Line use App\Employee; in EmployeeTransformer.php solved my problem.

Is this correct way to use Transformers?

UPDATE (after 30 minutes)

Finally I did it without editing composer.json.

app\Http\Controllers\EmployeeController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;

use Dingo\Api\Routing\Helpers;
use App\Employee;
use App\Transformers\EmployeeTransformer;

class EmployeeController extends Controller
{
    use Helpers;
    //
    //
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $employees = Employee::all();
        return $this->response->collection($employees, new EmployeeTransformer);
    }

}

app\Transformers\EmployeeTransformer.php:

<?php

namespace App\Transformers;

use League\Fractal\TransformerAbstract;
use App\Employee;

class EmployeeTransformer extends TransformerAbstract
{

    /**
     * Turn this item object into a generic array
     *
     * @return array
     */
    public function transform(Employee $employee)
    {
        return [
            'id'            => (int) $employee->id,
            'firstname'     => $employee->firstname,
            'lastname'      => $employee->lastname,
            'middlename'    => $employee->middlename
        ];
    }

}

I think wiki need update with examples :wink:

@Sogl The Latest update is the correct way to use Transformers

Was this page helpful?
0 / 5 - 0 ratings