How to create a RESTful web service with Yii 2

It’s important to remember that a REST web service is by definition a stateless service, this will imply some requirements in the way we will test things and deal with the information we need to POST or GET.

The big step forward that Yii made with version 2 can be seen in the built-in REST classes that provide an immediate solution once provided by third-party implementations.

This means we’ll have to introduce several changes to what we’ve achieved so far; the REST part of the application will be developed as a separate module, which will give us the ability to extend it and contain its logic. Because of this, the routes will be rearranged appropriately as well.

Before seeing what the Yii REST functionality is capable of doing, we’ll need to first have a quick look at modules in Yii, which we will use to develop our API to be tested.

Writing modular code in Yii

If you’ve never used modules since you’ve started working with Yii, well, I think it’s time to do so. Right now, modules are really easy and straightforward to use, and they will help you keep your code architecturally well organized and separated from the other components of your application.

Recommended :  5 BEST PHP FRAMEWORKS TO LEARN IN 2017

Modules are self-contained software units that contain models, views and controllers, and other software components, and the end user will be able to access the controller once it is installed in the main application. For this reason, modules are considered mini-applications, only difference being that they cannot live on their own. As an example, a forum or an administrative area can be developed as modules.

Modules can also be composed of submodules; a forum might have an admin submodule that contains all the logic and interfaces to moderate and customize the main forum module.

Modules can be quite complex in their structure; I would always strongly suggest an architectural analysis before deciding to keep everything under the same module, in the same way as you need to question your choices if you were to keep all the code in the same controller. Always try to keep in mind that you should be able to understand your code in one year’s time.

Recommended :  Add Double-opt-in registration for Laravel 5.1

Creating a module with Gii

Developing the REST interface using Yii modules is the easiest way to achieve versioning of the API. This way, we can easily switch and make an improved version of the API while still continuing to support the old version with minimal maintenance, until full deprecation.

So we will start with the creation of the module, using the web interface to the code generator called Gii. In case you skipped a few pages, the configuration for that is available in Chapter 4, Isolated Component Testing with PHPUnit, where you saw how to create a model with it.

Now, we will see how to create a module and what this will mean in terms of generated code.

So, head over to the Gii application, which in my case is http://basic.yii2.sandbox/gii and log in, if you are configured to do so and click on the Module Generator button.

The only two fields we have to fill in are these:

  • Module Class: This represents the main name-spaced class name of the module, which will be set to app\modules\v1\Module.
  • Module ID: This will be (automatically) set to v1.

Have a look at the following screenshot:

Creating a module with Gii

You can avoid creating the view by deselecting the related checkbox, as we’re not going to need one. We’re going to make more changes to what has been generated.

Recommended :  Dealing with modal directives in angularjs

Click on the Generate button, once ready.

Note

If your application will end up being more complex than what we have here, you still have a few options.

You can simply adjust the routes for the module, as explained in the documentation at http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html#adding-rules-dynamically.

Otherwise, you can create a module within a module (for example, a container module called api which will contain the various versions as modules such as v1, v2, and so on). Just remember to namespace it correctly when creating it. This is usually the solution I’d recommend from the code organization point of view.

The next step is to configure the module in order to be able to use it, and then we will see how to transform it into a REST module.

 



Pin It on Pinterest

Shares
Scroll Up

Get the best in web dev

Join dunebook.com and recieve best in web dev , once a week FREE

An email has been Sent to your Inbox ! Please Confirm your Subscription :)