CakePHP: Build REST APIs with RestApi plugin – Part 1

A guide to build REST APIs in CakePHP 3 application using RestApi plugin.

In this part, we will setup a fresh CakePHP application and install RestApi plugin.

Setup the application

In this step, we will install the CakePHP using composer. For this tutorial, I will use “C:\wamp64\www\cakephp-rest-api” directory.

We will use a virtual host for our CakePHP application. Let’s name it restapi.cake.dev and point it to C:\wamp64\www\cakephp-rest-api\webroot.

For detailed guide on installation and configuration, please see the official documentation here.

If everything goes well, you can see a default welcome page of CakePHP at http://restapi.cake.dev in your browser.

Setup the database

For our application, we will use MySQL database. For now, just create a blank database and name it restapi.

Now configure the database credentials in your APP/config/app.php. All set.

Refresh http://restapi.cake.dev in your browser and you will see a message that CakePHP is able to connect to the database.

You can skip this step if you do not need a database 🙂

Install the RestApi plugin

Our basic application is ready now. Let’s install our RestApi plugin using composer. Run the following command,

Once the installation is complete, load the plugin into your application by adding following line in your APP/config/bootstrap.php,

You can also load the plugin using in built shell command like,

That is it. We are now ready to go.

Plugin configuration

By default, the RestApi plugin is configured to allow CORS requests. You can override these settings by creating api.php file in APP/config. It should look like,

Modify the options as per your need.

A sample API method

Now everything is set and configured. Let’s create a first API method. Our endpoint will be http://restapi.cake.dev/foo/bar.

By default, CakePHP routing will look for Foo controller and bar() method for above url. So, let’s create FooCotroller first.

Please note that our FooController extends to ApiController and not the default AppController. This ApiController comes from RestApi plugin and we have added that on top of the class file. The ApiController provides all the necessary things required to make our API method working.

Now, run http://restapi.cake.dev/foo/bar and you will see following response.

This is because our plugin is set to check auth token in every request. You can disable this token check functionality either completely disabling it from configuration file or define a flag in your specific route.

Disable token check from configuration file

Update your api.php file and set ApiRequest.jwtAuth.enabled to false. So, your file will look like,

Disable token check from route

Add allowWithoutToken parameter in your route and set it to true.

After disabling auth token check, run http://restapi.cake.dev/foo/bar again and you will see following response.

Bingo! It works 🙂

Now, let’s prepare a valid response. Update our bar() action like,

And now it will return a list of movies in response.

The important thing to note here is $this->apiResponse. Anything you assign to this variable will be returned in response.

Easy enough, right?

You can put your own logic inside the action, set the response and the plugin will take care of rest.

That is it for part 1. In second part, we will learn the basic implementation of login/register APIs and auth token check.

Read the Part 2 of this tutorial here – CakePHP: Build REST APIs with RestApi plugin – Part 2

You may also like...