In this article you will read how to set up Application Programming Interfaces (APIs) via Laravel and Fractal. 

API Development is one of the things we are strongly committed to at Codana. We like to build strong APIs for different purposes.
When we use Laravel to build an application we always use Fractal.

There are a lot of examples in the documentation and on the internet how this is easy to implement in Laravel.
Here we have listed some recommended practices, which we use in some of our own projects.

What's Fractal for Laravel?

Fractal is a library written by the PHP League, a group that creates open source framework-independent libraries. To make this easy to use within Laravel applications a wrapper is provided.

You use fractal to return consistent responses from your API, to easily convert your data to JSON/YAML, because you send everything systematically through the same transformers you always make sure that data types are consistently the same. This layer between your output and your data models ensures that schema changes do not automatically get through to your API. So you monitor the backwards compatibility of your API much better.
 

Use the injected service to build responses.

There are facades to make this easier, but by doing so, your IDE can help you build your results.


public function __construct(Fractal $fractal)
{
	$this->fractal = $fractal;
}

public function respondWithData(User $user): JsonResponse
{
	return $this->fractal
      ->item($user)
      ->transformWith(new UserDetailsTransformer())
      ->respond();
}

 

Use includes where they are useful and reusable transformers.

It is very tempting to build up specific responses by giving extra data from a specific transformer.
In order to maintain as much consistent data as possible to the consumers of the API, it is recommended to reuse transformers where possible.

->parseIncludes('user.emailSettings')

 

Extra data is passed on to the constructor of the transformer.

If you need extra data in a transformer, you can give it to the constructor.

So don't do it this way.

// The bad way.
$this->fractal
  ->item(['data' => $data, 'currentUser' => $user])
  ->transformWith(new DataForUserTransformer());

It is clearer to do this, you can typehint on `transform` in the DataForUserTransformer (on the two arguments).
 

// The better way.
$this->fractal->collection($data)
      ->transformWith(new DataForUserTransformer($user));

 

Use a NullTransformer where necessary.

Sometimes you build an endpoint that is not strictly restful. For example, an endpoint where the average sales of the past week are calculated. In such cases, you can use a transformer that easily returns what you put in, without the need for other ways of building up responses.

final class NullTransformer extends TransformerAbstract
{
    public function transform(array $data): array
    {
        return $data;
    }
}

class Controller {

public function respondWithData(Request $request): JsonResponse
{
  $data = calculateForRequest($request);
  return $this->fractal
      ->item($data)
      ->transformWith(new NullTransformer())
      ->respond();
}
}

 

Eager loading is your best friend.

If you have configured fractal to always require an include, or otherwise display related data, make sure that the eager is loaded before you send it to fractal.

Adding eager loading before sending this data to fractal ensures that the data is already available in memory and does not need to be retrieved from the database on an item-by-item basis. This provides free performance gains for this endpoint.
 

Conclusion

Fractal always ensures that your code has a better structure and that your API is consistent and easy to consume. By using these tips you can have even more fun.

Author: Joris Vercammen
Developer
Joris Vercammen

More insights

Cross-platform applicaties with React Native

Never before has developing native mobile applications been as accessible as it is today. At Codana, we do this by using the React Native, an open-source framework developed by Meta.

Author: Jinse Camps
Architect | Analyst
Jinse Camps
dev

Laracon EU 2024

A fantastic learning experience to inspire and be inspired together with a lot of other Laravel passionate people! Something we couldn't miss and very much connect with the community. What a top event! Who will we see next editions? 😮

Author: Noah Gillard
PHP / Laravel Developer
Noah Gillard AI generated Face
laracon codana persoon

An efficient tourism data management system

A TDMS or Tourist Data Management System, is simply a platform that retrieves data from various sources, processes it internally either automatically or not, and offers this data back to external platforms.

Author: Tom Van den Eynden
Web Architect | Coordinator
Tom Van den Eynden
laptop

Tourism Data Management Systems

In dit artikel verkennen we wat een TDMS is, waarom het essentieel is voor de toerisme-industrie, en hoe technologieën zoals Laravel en ElasticSearch het verschil kunnen maken. 

Author: Tom Van den Eynden
Web Architect | Coordinator
Tom Van den Eynden
tdms

The difference between data management and data processing in a digital economy

Gegevens zijn cruciaal voor bedrijven en het begrijpen van de verschillen tussen gegevensbeheer en gegevensverwerking kan verwarrend zijn. In dit artikel zullen we deze verschillen in de digitale economie nader bekijken om hun doelen en toepassingen beter te begrijpen.

Author: Tom Van den Eynden
Web Architect | Coordinator
Tom Van den Eynden
gegevensverwerking

Test Driven Development - application to a project

TDD, or in full Test Driven Development, is an approach to development where we start from writing tests.

Author: Sarah Jehin
PHP developer
Sarah Jehin
development