![]() ![]() Return $this->collection(Variants::all(), new VariantsTransformer) Return variants via the Variants Transformer. ![]() In the VariantsController, we need to change our functions to adopt the new transformer use App\Transformers\V1\VariantsTransformer public function index() This means that if there’s a major database update that needs to take place, you can update the transformer and not have to ask customers to re-map everything in their APIs. The next key advantage is that the variant database field name doesn’t have to match the API field name. Only items in this array will be returned in API requests. What this empowers you to do is hide database fields – like created at – so there’s no risk they’ll be visible to the API. The right hand side gets the variant fields from the database. The left hand side of the array define the keys that will be used for the JSON response. Open that file in Atom and lets make our transformer (int) $variant->id,Īll we’re doing here is transforming our Database collection into an array and returning it. Now lets make a new VariantsTransformer.php file in that directory: touch app/Transformers/V1/VariantsTransformer.php Instead, I think we should make a new folder in the app directory, and lets version it too, incase different versions of the API use different transformers mkdir app/Transformers At the moment we should have Http/Controllers/api and all of the controllers should be within this. It doesn’t really make sense to put transformers here, as they’re not controllers. I’m going to start by looking at my project structure. ![]() All we do here is get variants, we don’t allow them to be added, updated or deleted. Once again, I’m going to start with the Variants as this is the smallest part of the API. We can change the database field names without worrying about what users are doing. Additionally, the abstraction here means that if we change our database architecture we’re not relying on API users to change their tools or utilities as well. This obscures our database architecture so we’re not giving away our field names. Instead of the API returning this to the user on a get request, we could use a transformer to return a different field name “name” for example. Say in our database we were storing a field “recipient_name”. What this means is we “transform” what our database record field names are, and turn them into something else. Transformers are often used in APIs to obscure and abstract the database later from the responses provided to users. We’re not validating our data before adding it to the database.Our index functions are using “all()”, so they get all results from the database.We’re exposing our database architecture – people can see orders have fields “order_ref”, “recipient_id”, etc.Okay, so the last few lessons have got us up to the point where we’re able to send and receive data to the API, but there are some problems that need to be thought about: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |