Answer the question
In order to leave comments, you need to log in
How to correctly organize the Symfony2 service architecture to support multiple versions of the API at the same time?
Hello.
I looked at this material on how to organize the work of several APIs at the same time. The most suitable method for me is the transformation of Request requests to form the data structure necessary for the service. Business logic services are always the same and change only towards the current api version. The question remains only how to organize such a structure in PHP, and in particular, in Symfony?
Those. as I imagine it:
- the transformer in the controller will be used, when receiving the date from requesta, the transformer validates this data for the api version that this controller processes, then, if the data is correct, the transformer transforms this data into those that will be necessary for work our service.
- and a validator will be used in the service that validates the data, already directly in the service.
For example:
In the first version of the API, when creating a profile, we used the first_name, last_name, patronymic fields.
In the second version of the API, we only use the name_info field, which contains the full name information. Accordingly, the service will work with us with the new version and will accept an array with the $data['name_info'] key as input, which will validate.
class ProfileFactory
{
public function create(array $data){
// throwing some ValidateException
$this->validator->validate(CreationValidateRules(), $data);
$profile = new Profile();
$profile->setNameInfo($data['name_info']); // Вот тут смущает то, что сервис и валидатор знают о ключах массива отдельно друг от друга
//.... some code
return $profile;
}
}
class ProfileControllerApi_2{
public function createProfile(Request $request){
$data = $request->request->all();
$profile = $this->get('profile_factory')->create($data );
// ... etc.
}
}
class ProfileControllerApi_1{
public function createProfile(Request $request){
$data = $request->request->all();
//throwing some ValidateException
$transformedData = $this->transformerHandler->transform(ProfileCreatingTransformer(), $data);
$profile = $this->get('profile_factory')->create($transformedData );
// ... etc.
}
}
Answer the question
In order to leave comments, you need to log in
The question remains only how to organize such a structure in PHP, and in particular, in Symfony?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question