Support the ongoing development of Laravel.io →
posted 10 years ago
Architecture
Last updated 2 years ago.
0

Hi!

I've just skimmed your GitHub. If I was right, your Serializer and Api class is just for expose the model's data and render the data as json? If that, Laravel itself have 2 interfaces: Illuminate\Support\ArrayableInterface, Illuminate\Support\JsonableInterface (and the Eloquent was already implemented them). In your Controller's action, if you return any instance of them, then it'll be rendered as json.

And if you still not satisfy with them, why stop there, define your Serializable interface:

interface Serializable {
    /**
     * @return array;
     */
    public function serialize();
}

Then your model or anything that you want to send to the client as json just implement it?

and here is where the magic happend:

The Response::macro() http://laravel.com/docs/responses#response-macros

Response::macro('serialize', function(Serializable $model)
{
    return Response::json($model->serialize());
});

And now, in your controller:

return Response::serialize($model);

And again, why stop there? Let's define your notFound macro, internalError macro...

To sum up, you can using those interfaces and the Response::macro() to do your stuff, say bye bye to the Api and Serializer :)

Last updated 2 years ago.
0

I unterstand your approach but I can't make it works.

$exercises = $this->exercises->all();
return Response::serialize($exercises);

Return error Argument 1 passed to {closure}() must implement interface Serializable, instance of Illuminate\Database\Eloquent\Collection given. I don't have model object in this situation.

Last updated 2 years ago.
0

Well, they are just the idea, you can implement it by your own style.

This script:

$this->exercises->all();

Is returning a collection of your model, and (why stop there ?) you can have a "serializeCollection" macro, or overwrite the all() method to return your collection type, not the Eloquent collection, and your collection type implements the serialize method.

Last updated 2 years ago.
0

I have managed to deal with that by creating repositories to communicate my controller with models. Now, my controllers look likes this

use Storage\Engine\EngineRepository as Engine;

class EnginesController extends \BaseController {

	/**
	 * Insert dependencies
	 * @param EngineRepository $engine
	 */
	public function __construct(Engine $engine)
	{
		$this->engine = $engine;
	}

	/**
	 * Display a listing of engines
	 *
	 * @return Response
	 */
	public function index()
	{
		$engines = $this->engine->getAllSerialized();

		return $engines;
	}

	/**
	 * Display the specified engine.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$engine = $this->engine->getOneSerialized($id);

		return $engine;
	}

}
<?php namespace Storage\Engine;

use Engine;
use Serializers\EngineSerializer;

class EloquentEngineRepository implements EngineRepository {

	public function __construct(EngineSerializer $engineSerializer)
	{
		$this->engineSerializer = $engineSerializer;
	}

	public function getAllSerialized()
	{
		$engines = Engine::all()->toArray();

		return ["engines" => $this->engineSerializer->transformCollection($engines)];
	}

	public function getOneSerialized($id)
	{
		$engines = Engine::findOrFail($id)->toArray();

		return ["engine" => $this->engineSerializer->transform($engines)];
	}

}

Everythink is clear and readable.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

estshy estshy Joined 2 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.