Chapter 11. Best Practices and Conventions

This chapter will teach you to stand out among the rest of the developers. This is possible by developing and executing the strategies learned in this book with style, and by following concrete standards.

Code versioning best practices

Over time, your application will evolve and, at some point, you will be at the point where you are wondering what you will do with the API of any of your microservices. You can keep the changes to a minimum and be transparent to the users of your API, or you can create different versions of your code. The best solution is versioning your code (API).

The well-known and commonly used ways for code versioning are as listed:

  • URL: In this method, you add the version of your API inside the URL of the requests. For example, the https://phpmicroservices.com/api/v2/user URL indicates that we are using the v2 of our API. We used this method in our examples throughout the book.
  • Custom request header: In this method, we do not specify the version in our URL. Instead, we use HTTP headers to specify the version we want to use. For instance, we can do a HTTP call to https://phpmicroservices.com/api/user, but with an extra header, "api-version: 2". In this case, our server will check the HTTP header and use the v2 of our API.
  • Accept header: This method is very similar to the previous one, but instead of using a custom header, we will use the Accept header. For our example, we will make a call to https://phpmicroservices.com/api/user but our Accept header will be "Accept: application/vnd.phpmicroservices.v2+json". In this case, we are indicating that we want version 2 and the data will be on JSON.

As you can imagine, the easiest way to implement versioning in your code is with the version code inside your URL but, unfortunately, that is not considered the best option. What most developers consider the best way of versioning your code is to use HTTP headers to specify the version you want to use. Our recommendation is to use the method that suits your project best. Analyze who will use your APIs and how, and you will discover the versioning method you need to use.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.195.97