search
HomeBackend DevelopmentPHP TutorialIntroduction to version control of Yii2.0 RESTful API (code example)
Introduction to version control of Yii2.0 RESTful API (code example)Jan 10, 2019 am 11:27 AM
apiphprestfulyii2version control

This article brings you an introduction to the version control of Yii2.0 RESTful API (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

I have written two articles before about how to build Yii2.0 RESTful API, as well as authentication and other processing, but they did not involve version management. Today I will talk about how to implement version management.

Let’s just start from scratch and build it step by step. However, some concepts and usage will not be explained one by one in this article. You can refer to the first Yii2.0 RESTful API basic configuration tutorial for configuration

Install Yii2.0

Install via Composer

This is the preferred method to install Yii2.0. If you don't have Composer installed yet, you can follow the instructions here to install it.

After installing Composer, run the following command to install the Composer Asset plug-in:

composer global require "fxp/composer-asset-plugin:^1.2.0"

To install the advanced application template, run the following command:

composer create-project yiisoft/yii2-app-advanced yii-api 2.0.14

Copy backend directory, named api

Open api\config\main.php and modify the id, controllerNamespace:

return [
    'id' => 'app-api',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'api\controllers',
]

Initialize the advanced template

Before initializing, you may wish to read this article

cd advanced
php init

Open common\config\main.php to enable url routing beautification rules

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
    ],
],

Open common\config\bootstrap.php and add the following alias

Yii::setAlias('@api', dirname(dirname(__DIR__)) . '/api');

ok, the above work is ready, let’s get to the point,
For more information about the version, you can refer to the authoritative guide, but I won’t explain too much here (PS: Mainly, I don’t know how to...)

My understanding:
You can understand the versions of Yii2 as different modules. Each version is a new module, such as the common v1, v2, etc.

Building the module

Regarding how to generate the module, we can use GII to generate it.

Configuring GII

Open the api/config/main-local.php file and modify it as follows:

if (!YII_ENV_TEST) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
    ];

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
        'allowedIPs' => ['127.0.0.1', '*']
    ];
}

Because I am using Homestead, access to GII is not allowed by default, so I have to add 'allowedIPs' => ; ['127.0.0.1', '*'], otherwise Forbidden (#403) will appear. You can configure it according to your own needs, or do not configure it

Generate Modules

Browse Enter http://your host/gii into the generator, you can see the Module Generator, click Start

Modules Class and enter: api\modules\v1\Module

Module ID and enter v1, (Generally it will be entered automatically)

Click Preview

Introduction to version control of Yii2.0 RESTful API (code example)

##Finally click Generate to generate

Introduction to version control of Yii2.0 RESTful API (code example)

Configuration module

Open the api/config/main.php file, modify modules

'modules' => [
    'v1'=>[
        'class'=>'api\modules\v1\Module',
    ],
],
Then modify urlManager

'urlManager' => [
    'enablePrettyUrl' => true,
    'enableStrictParsing' => true,
    'showScriptName' => false,
    'rules' => [
        ['class' => 'yii\rest\UrlRule',
            'controller' => 'v1/default',
            'extraPatterns'=>[
                'GET index'=>'index',
            ],
        ],
    ],
],
Based on the above , Yii2.0 RESTFul API implements version management, we can access it through the following address:

http://localhost/v1/defaults
To say a little more, the address above me has been mapped to the api/web directory, please base it on your actual situation Configure
Open the modules file directory just generated. You can see that there is a v1 directory in it. You can see that the directory also has a controllers and a views directory

. In fact, the defaults we just visited These two files are the same as traditional web projects. The controller renders the view

Okay, you may know that our future controller code will be placed in modules/v1/controllers

Just now is just the code generated by the default GII for us. Because we are an API, we generally don't use the views directory.

Create a rest controller

Create a new UserController under modules\v1\controllers

<?php namespace api\modules\v1\controllers;

use yii\rest\Controller;

/**
 * User controller for the `v1` module
 */
class UserController extends Controller
{
    /**
     * @return string
     */
    public function actionIndex()
    {
        return &#39;this is v1/user&#39;;
    }
}
Modify the urlManager in api/config/main.php

'urlManager' => [
    'enablePrettyUrl' => true,
    'enableStrictParsing' => true,
    'showScriptName' => false,
    'rules' => [
        ['class' => 'yii\rest\UrlRule',
            'controller' => 'v1/default',
            'extraPatterns'=>[
                'GET index'=>'index',
            ],
        ],
        ['class' => 'yii\rest\UrlRule',
            'controller' => 'v1/user',
            'extraPatterns'=>[
                'GET index'=>'index',
            ],
        ],
    ],
],
Try Next visit

http://localhost/v1/users/index
ok, the above is the implementation method of Yii2.0 version management

Formatted response

Modify api/config/main. php add response

'response' => [
    'class' => 'yii\web\Response',
    'on beforeSend' => function ($event) {
        $response = $event->sender;
        $response->data = [
            'success' => $response->isSuccessful,
            'code' => $response->getStatusCode(),
            'message' => $response->statusText,
            'data' => $response->data,
        ];
        $response->statusCode = 200;
    },
],

in the components array

The above is the detailed content of Introduction to version control of Yii2.0 RESTful API (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
PHP开发中如何使用Git进行版本控制PHP开发中如何使用Git进行版本控制Jun 27, 2023 pm 02:48 PM

随着互联网的快速发展,PHP已经成为了众多网站开发中的主流语言。众所周知,版本控制是软件开发中不可或缺的一个重要环节,而Git作为目前最流行的版本控制工具之一,其强大的功能和易用性受到了广大开发者的欢迎。本文将介绍如何在PHP开发中使用Git进行版本控制。一、Git基础知识在使用Git进行版本控制之前,我们需要对Git的基础知识有所了解。Git的三个工作区在

PHP开发中如何使用SVN进行版本控制PHP开发中如何使用SVN进行版本控制Jun 27, 2023 pm 01:39 PM

在PHP开发中进行版本控制是很常见的操作,其中最常用的工具就是SVN(Subversion)。它可以方便地管理代码的历史版本以及协同开发过程中的代码更新。下面将介绍如何在PHP开发中使用SVN进行版本控制。一、安装SVN客户端和服务端首先需要安装SVN客户端和服务端。SVN客户端可以在SVN官网上下载对应的版本,安装即可,而服务端则需要自行搭建,具体方法可以

MySql的多版本管理:如何快速管理MySQL的多个版本MySql的多版本管理:如何快速管理MySQL的多个版本Jun 16, 2023 am 08:11 AM

MySQL是一种广泛应用于各种不同领域的数据库管理系统。在开发过程中,开发人员可能需要使用不同的MySQL版本来测试和开发。然而,如何轻松地管理MySQL的多个版本是一个挑战,特别是在一个项目中涉及多个版本时。本文将介绍一些快速管理MySQL多个版本的最佳实践。使用虚拟机使用虚拟机是管理多个MySQL版本的最常用和最简单的方法之一。虚拟机允许我们在单个计算机

Java开发中如何进行版本控制和代码管理Java开发中如何进行版本控制和代码管理Oct 09, 2023 am 08:46 AM

Java开发中如何进行版本控制和代码管理,需要具体代码示例摘要:随着项目规模的扩大和团队协作的需要,版本控制和代码管理成为了Java开发中至关重要的方面。本文将介绍版本控制的概念、常用的版本控制工具,以及如何进行代码管理。同时,还将提供具体的代码示例以帮助读者更好地理解和实践。一、版本控制的概念版本控制是一种记录文件内容变化的方式,以便将来查阅特定版本的文件

如何进行C++代码的版本控制?如何进行C++代码的版本控制?Nov 02, 2023 pm 04:35 PM

如何进行C++代码的版本控制?引言:随着软件开发的不断发展,代码的版本管理变得至关重要。版本控制是一种管理和跟踪代码变化的机制,旨在提高代码开发和维护的效率。对于C++开发人员来说,版本控制是不可或缺的工具,本文将介绍如何进行C++代码的版本控制,以帮助开发人员更好地管理和跟踪代码变化。一、选择合适的版本控制系统在开始进行C++代码的版本控制之前,首先需要选

Java开发中如何进行代码版本管理和发布Java开发中如何进行代码版本管理和发布Oct 10, 2023 pm 10:06 PM

Java开发中如何进行代码版本管理和发布在Java开发中,代码版本管理和发布非常重要。它可以帮助团队协作开发,保持代码的安全性和稳定性,并使代码的迭代和发布更加高效。本文将介绍几种常用的代码版本管理工具,并提供具体的代码示例。GitGit是目前最流行的分布式版本控制系统,广泛应用于Java开发中。它可以记录每一次代码的修改,并提供强大的分支管理功能。Git使

如何使用 PHP 实现自动更新和版本控制功能如何使用 PHP 实现自动更新和版本控制功能Sep 05, 2023 pm 02:28 PM

如何使用PHP实现自动更新和版本控制功能概述:在开发网站和应用程序时,经常会遇到需要更新版本以修复漏洞或添加新功能的情况。手动更新和版本控制可能会比较繁琐和容易出错,因此我们可以利用PHP实现自动更新和版本控制功能。本文将介绍如何使用PHP和一些常用的工具来实现这些功能。步骤一:设置版本信息首先,在项目根目录中创建一个名为"version.jso

Java 中的代码管理和版本控制技术Java 中的代码管理和版本控制技术Jun 08, 2023 am 08:09 AM

Java是一种流行的编程语言,开发者们在开发Java应用程序时,需要考虑到代码的管理和版本控制技术。这是因为随着软件的开发,代码量会持续增长,版本控制系统可以帮助开发者有效地跟踪代码的变化,并避免出现不必要的错误和冲突。本文将介绍Java中常用的代码管理和版本控制技术。代码管理技术代码管理技术是指在软件开发过程中,能够帮助开发者组织和管理代码的工具

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)