In this chapter, you will learn how to create RESTful Web Services with the new integrated management of Yii 2.
You will learn how to create a new application to manage the api
environment and how to create a controller using the default base classes provided by the framework.
Then, we will cover authentication methods and you'll learn how to customize the response output format. We'll also discuss:
Before using the advanced template, it is advisable to configure RESTful Web Services, since, as you saw in previous chapters, this configuration allows you to easily add a new application in the same project.
Yii provides many built-in features to create RESTful Web Services and it reduces the code needed to implement it that is always structured with models, controllers, and actions.
These are its main features:
index
, view
, create
, update
, delete
, and options
) in yii
estActiveController
, which is the base controller suggested to overrideYii applies well-established knowledge about RESTful Web Services creation, such as how to present metadata in the response output. So, it is advisable that we follow the framework guidelines as far as possible; in this way, we will write commonly manageable REST APIs.
The first thing to do with an advanced template is to create a new application in the same project, for example renaming it api
. Yii has not got a built-in functionality to create a new application, but it only takes a few steps to complete this task.
Starting from the root of our project, we will create, as well as for other applications (common
, backend
, frontend
, and console
), a new folder named api
with the following command:
$ mkdir api
Now, enter in api
and let's create these five subfolders:
$ mkdir config $ mkdir web $ mkdir controllers $ mkdir runtime
We must only create files for the first two folders, and the others will be left temporarily empty.
In the config
folder, we must create two files: main.php
and params.php
. The second file, params.php
, will be temporarily empty as we have not got any parameters to store in it, such as:
<?php return [ ];
The content of api/config/main.php
will, instead, be:
<?php $params = array_merge( require(__DIR__ . '/../../common/config/params.php'), require(__DIR__ . '/../../common/config/params-local.php'), require(__DIR__ . '/params.php') ); return [ 'id' => 'app-api', 'basePath' => dirname(__DIR__), 'controllerNamespace' => 'apicontrollers', 'bootstrap' => ['log'], 'modules' => [], 'components' => [ 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, ], 'user' => [ 'identityClass' => 'commonmodelsUser', 'enableSession' => false, 'loginUrl' => null ], 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ [ 'class' => 'yiilogFileTarget', 'levels' => ['error', 'warning'], ], ], ], ], 'params' => $params, ];
Then, we will create an index.php
file in the web
folder with the following content:
<?php defined('YII_DEBUG') or define('YII_DEBUG', true); defined('YII_ENV') or define('YII_ENV', 'dev'); require(__DIR__ . '/../../vendor/autoload.php'); require(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php'); require(__DIR__ . '/../../common/config/bootstrap.php'); $config = yiihelpersArrayHelper::merge( require(__DIR__ . '/../../common/config/main.php'), require(__DIR__ . '/../../common/config/main-local.php'), require(__DIR__ . '/../config/main.php') ); $application = new yiiwebApplication($config); $application->run();
Still in the web
folder, we will create the .htaccess
file to handle a pretty URL:
RewriteEngine on # If a directory or a file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Otherwise forward it to index.php RewriteRule . index.php
Finally, we have to add a new alias in common/config/bootstrap
regarding the api
application:
Yii::setAlias('api', dirname(dirname(__DIR__)) . '/api');
Our job is complete, as we finally have a brand new application from scratch.
18.119.105.239