


Environment configuration:
Enable server pseudo-static
This article takes apache as an example. Check httpd.conf in apache's conf directory and find the following code
LoadModule rewrite_module modules/mod_rewrite.so
Remove the # in front of it, and add it if not found .
Find the code
<directory><span> AllowOverride All Options None </span><span>Require</span><span> all granted </span></directory>
Change the original AllowOverride None to AllowOverride All.
Then create an .htaccess file in the root directory of the site with the following content:


<ifmodule mod_rewrite.c><span> Options </span>+<span>FollowSymlinks RewriteEngine On RewriteCond </span>%{REQUEST_FILENAME} !-<span>d RewriteCond </span>%{REQUEST_FILENAME} !-<span>f RewriteRule </span>. index.<span>php </span></ifmodule>.htaccess
The configuration of yii2 will not be described here. If necessary, you can read the YII2 Practical Manual .
YII2 actual operation:
1. Configure URL rules and modules
(1) Create a new modules folder and implement api interface version control. For example, V1 version, V2 version...
Create a new controllers folder (controller), models folder (model), and Module.php configuration file under the v1 folder.
Module.php file is as follows:
<span> 1</span> <span>php </span><span> 2</span><span>namespace api\modules\v1; </span><span> 3</span><span> 4</span><span>class</span> Module <span>extends</span><span> \yii\base\Module </span><span> 5</span><span>{ </span><span> 6</span><span> 7</span><span>public</span><span>$controllerNamespace</span> = 'api\modules\v1\controllers'<span>; </span><span> 8</span><span> 9</span><span>public</span><span>function</span><span> init() </span><span>10</span><span> { </span><span>11</span> parent::<span>init(); </span><span>12</span><span> } </span><span>13</span> }
Lines 2 and 7 change with version expansion (v1->v2...).
(2) Configure the main.php file under the config folder


<span> 1</span> <span>php </span><span> 2</span><span>$params</span> = <span>array_merge</span>(<span>require</span> (__DIR__ . '/../../common/config/params.php'), <span>require</span> (__DIR__ . '/../../common/config/params-local.php'), <span>require</span> (__DIR__ . '/params.php'), <span>require</span> (__DIR__ . '/params-local.php'<span>)); </span><span> 3</span><span> 4</span><span>return</span><span> [ </span><span> 5</span> 'id' => 'app-api', <span> 6</span> 'basePath' => <span>dirname</span>(__DIR__), <span> 7</span> 'bootstrap' =><span> [ </span><span> 8</span> 'log' <span> 9</span> ], <span>10</span> 'modules' =><span> [ </span><span>11</span> 'v1' =><span> [ </span><span>12</span> 'class' => 'api\modules\v1\Module' <span>13</span> ], <span>14</span> 'v2' =><span> [ </span><span>15</span> 'class' => 'api\modules\v2\Module' <span>16</span><span> ] </span><span>17</span> ], <span>18</span> 'controllerNamespace' => 'api\controllers', <span>19</span> 'components' =><span> [ </span><span>20</span> 'user' =><span> [ </span><span>21</span> 'identityClass' => 'common\models\User', <span>22</span> 'enableAutoLogin' => <span>false</span>, <span>23</span> 'enableSession' => <span>false</span>, <span>24</span> 'loginUrl' => <span>null</span><span>25</span> ], <span>26</span> 'urlManager' =><span> [ </span><span>27</span> 'enablePrettyUrl' => <span>true</span>, <span>//</span><span> 启用美化URL</span><span>28</span> 'enableStrictParsing' => <span>true</span>, <span>//</span><span> 是否执行严格的url解析</span><span>29</span> 'showScriptName' => <span>false</span>, <span>//</span><span> 在URL路径中是否显示脚本入口文件</span><span>30</span> 'rules' =><span> [ </span><span>31</span><span> [ </span><span>32</span> 'class' => 'yii\rest\UrlRule', <span>33</span> 'controller' =><span> [ </span><span>34</span> 'v1/site' <span>35</span><span> ] </span><span>36</span> ], <span>37</span><span> [ </span><span>38</span> 'class' => 'yii\rest\UrlRule', <span>39</span> 'controller' =><span> [ </span><span>40</span> 'v2/site' <span>41</span><span> ] </span><span>42</span><span> ] </span><span>43</span><span> ] </span><span>44</span> ], <span>45</span> 'log' =><span> [ </span><span>46</span> 'traceLevel' => YII_DEBUG ? 3 : 0, <span>47</span> 'targets' =><span> [ </span><span>48</span><span> [ </span><span>49</span> 'class' => 'yii\log\FileTarget', <span>50</span> 'levels' =><span> [ </span><span>51</span> 'error', <span>52</span> 'warning' <span>53</span><span> ] </span><span>54</span><span> ] </span><span>55</span><span> ] </span><span>56</span> ], <span>57</span> 'errorHandler' =><span> [ </span><span>58</span> 'errorAction' => 'site/error' <span>59</span><span> ] </span><span>60</span> ], <span>61</span> 'params' => <span>$params</span><span>62</span> ];main.php
Pay attention to the component configuration of lines 10~17 and 20~44. I believe you can understand it if you read it carefully , I won’t go into details here. Please pay special attention to lines 33 to 35 of the code. This represents the v1/site controller. As the number of interface controllers increases, you can directly add them to the array. This article strives to quickly configure the implementation of RESTful architecture.
(3) v2 and v3 represent future version changes, and the configuration is similar to the v1 folder.
2. Create a model
Prepare a data table named mxq_guide in the database
<span>CREATE TABLE `mxq_guide` ( `id` int(</span>11) NOT <span>NULL</span> AUTO_INCREMENT,<span> `imgurl` varchar(</span>255) <span>DEFAULT</span><span>NULL </span>COMMENT '图片路径'<span>,</span><span><br> `status` int(</span><span>11) </span><span>DEFAULT</span><span>NULL</span><span> COMMENT '1启用 0禁用',<br></span><span> `flag` int(</span><span>11) </span><span>DEFAULT</span><span>NULL</span><span> COMMENT '1安卓 2苹果',</span><span><br> PRIMARY </span><span>KEY</span><span> (`id`) <br> ) ENGINE</span><span>=MyISAM AUTO_INCREMENT=24 </span><span>DEFAULT</span><span> CHARSET=utf8 COMMENT='APP导航图';</span>
After creation, please pay attention to add several pieces of data information to the database in time.
Create the guide.php model through scaffolding gii (see the yii2 authoritative guide for usage instructions). Pay attention to rewriting the generated file and modify it into the following form to meet RESTful requirements. Then move from the models folder to the v1/models folder and pay attention to the modification of the namespace.


<span> 1</span> <span>php </span><span> 2</span><span>namespace api\modules\v1\models; </span><span> 3</span><span> 4</span><span>use</span><span> Yii; </span><span> 5</span><span>use</span><span> yii\db\ActiveRecord; </span><span> 6</span><span>use</span><span> yii\web\IdentityInterface; </span><span> 7</span><span> 8</span><span>/*</span><span>* </span><span> 9</span><span> * This is the model class for table "{{%guide}}". </span><span>10</span><span> * </span><span>11</span><span> * @property integer $id </span><span>12</span><span> * @property string $imgurl </span><span>13</span><span> * @property integer $status </span><span>14</span><span> * @property integer $flag </span><span>15</span><span>*/</span><span>16</span><span>class</span> Guide <span>extends</span> ActiveRecord <span>implements</span><span> IdentityInterface </span><span>17</span><span>{ </span><span>18</span><span>19</span><span>public</span><span>static</span><span>function</span> findIdentityByAccessToken(<span>$token</span>, <span>$type</span> = <span>null</span><span>) </span><span>20</span><span> { </span><span>21</span><span>return</span><span>static</span>::<span>findOne([ </span><span>22</span> 'access_token' => <span>$token</span><span>23</span><span> ]); </span><span>24</span><span> } </span><span>25</span><span>26</span><span>public</span><span>function</span><span> getId() </span><span>27</span><span> { </span><span>28</span><span>return</span><span>$this</span>-><span>id; </span><span>29</span><span> } </span><span>30</span><span>31</span><span>public</span><span>function</span><span> getAuthKey() </span><span>32</span><span> { </span><span>33</span><span>return</span><span>$this</span>-><span>authKey; </span><span>34</span><span> } </span><span>35</span><span>36</span><span>public</span><span>function</span> validateAuthKey(<span>$authKey</span><span>) </span><span>37</span><span> { </span><span>38</span><span>return</span><span>$this</span>->authKey === <span>$authKey</span><span>; </span><span>39</span><span> } </span><span>40</span><span>41</span><span>public</span><span>static</span><span>function</span> findIdentity(<span>$id</span><span>) </span><span>42</span><span> { </span><span>43</span><span>return</span><span>static</span>::findOne(<span>$id</span><span>); </span><span>44</span><span> } </span><span>45</span><span>46</span><span>public</span><span>static</span><span>function</span><span> tableName() </span><span>47</span><span> { </span><span>48</span><span>return</span> '{{%guide}}'<span>; </span><span>49</span><span> } </span><span>50</span><span>51</span><span>public</span><span>function</span><span> rules() </span><span>52</span><span> { </span><span>53</span><span>return</span><span> [ </span><span>54</span><span> [ </span><span>55</span><span> [ </span><span>56</span> 'imgurl', <span>57</span> 'status', <span>58</span> 'flag' <span>59</span> ], <span>60</span> 'required' <span>61</span> ], <span>62</span><span> [ </span><span>63</span><span> [ </span><span>64</span> 'status', <span>65</span> 'flag' <span>66</span> ], <span>67</span> 'integer' <span>68</span> ], <span>69</span><span> [ </span><span>70</span><span> [ </span><span>71</span> 'imgurl' <span>72</span> ], <span>73</span> 'string', <span>74</span> 'max' => 255 <span>75</span><span> ] </span><span>76</span><span> ]; </span><span>77</span><span> } </span><span>78</span><span>79</span><span>public</span><span>function</span><span> attributeLabels() </span><span>80</span><span> { </span><span>81</span><span>return</span><span> [ </span><span>82</span> 'id' => Yii::t('app', 'ID'), <span>83</span> 'imgurl' => Yii::t('app', 'imgurl'), <span>84</span> 'status' => Yii::t('app', 'status'), <span>85</span> 'flag' => Yii::t('app', 'flag'<span>) </span><span>86</span><span> ]; </span><span>87</span><span> } </span><span>88</span> }guide.php
3. Create a controller


<span> 1</span> <span>php </span><span> 2</span><span>namespace api\modules\v1\controllers; </span><span> 3</span><span> 4</span><span>use</span><span> Yii; </span><span> 5</span><span>use</span><span> yii\rest\ActiveController; </span><span> 6</span><span>use</span><span> yii\filters\auth\CompositeAuth; </span><span> 7</span><span>use</span><span> yii\filters\auth\QueryParamAuth; </span><span> 8</span><span>use</span><span> yii\data\ActiveDataProvider; </span><span> 9</span><span>10</span><span>class</span> SiteController <span>extends</span><span> ActiveController </span><span>11</span><span>{ </span><span>12</span><span>13</span><span>public</span><span>$modelClass</span> = 'api\modules\v1\models\guide'<span>; </span><span>14</span><span>15</span><span>public</span><span>$serializer</span> =<span> [ </span><span>16</span> 'class' => 'yii\rest\Serializer', <span>17</span> 'collectionEnvelope' => 'items' <span>18</span><span> ]; </span><span>19</span><span>20</span><span>//</span><span> public function behaviors() </span><span>21</span><span> // { </span><span>22</span><span> // $behaviors = parent::behaviors(); </span><span>23</span><span> // $behaviors['authenticator'] = [ </span><span>24</span><span> // 'class' => CompositeAuth::className(), </span><span>25</span><span> // 'authMethods' => [ </span><span>26</span><span> // QueryParamAuth::className() </span><span>27</span><span> // ] </span><span>28</span><span> // ]; </span><span>29</span><span> // return $behaviors; </span><span>30</span><span> // }</span><span>31</span><span>public</span><span>function</span><span> actions() </span><span>32</span><span> { </span><span>33</span><span>$actions</span> = parent::<span>actions(); </span><span>34</span><span>//</span><span> 注销系统自带的实现方法</span><span>35</span><span>unset</span>(<span>$actions</span>['index'], <span>$actions</span>['update'], <span>$actions</span>['create'], <span>$actions</span>['delete'], <span>$actions</span>['view'<span>]); </span><span>36</span><span>return</span><span>$actions</span><span>; </span><span>37</span><span> } </span><span>38</span><span>39</span><span>public</span><span>function</span><span> actionIndex() </span><span>40</span><span> { </span><span>41</span><span>$modelClass</span> = <span>$this</span>-><span>modelClass; </span><span>42</span><span>$query</span> = <span>$modelClass</span>::<span>find(); </span><span>43</span><span>return</span><span>new</span><span> ActiveDataProvider([ </span><span>44</span> 'query' => <span>$query</span><span>45</span><span> ]); </span><span>46</span><span> } </span><span>47</span><span>48</span><span>public</span><span>function</span><span> actionCreate() </span><span>49</span><span> { </span><span>50</span><span>$model</span> = <span>new</span><span>$this</span>-><span>modelClass(); </span><span>51</span><span>//</span><span> $model->load(Yii::$app->getRequest() </span><span>52</span><span> // ->getBodyParams(), '');</span><span>53</span><span>$model</span>->attributes = Yii::<span>$app</span>->request-><span>post(); </span><span>54</span><span>if</span> (! <span>$model</span>-><span>save()) { </span><span>55</span><span>return</span><span>array_values</span>(<span>$model</span>->getFirstErrors())[0<span>]; </span><span>56</span><span> } </span><span>57</span><span>return</span><span>$model</span><span>; </span><span>58</span><span> } </span><span>59</span><span>60</span><span>public</span><span>function</span> actionUpdate(<span>$id</span><span>) </span><span>61</span><span> { </span><span>62</span><span>$model</span> = <span>$this</span>->findModel(<span>$id</span><span>); </span><span>63</span><span>$model</span>->attributes = Yii::<span>$app</span>->request-><span>post(); </span><span>64</span><span>if</span> (! <span>$model</span>-><span>save()) { </span><span>65</span><span>return</span><span>array_values</span>(<span>$model</span>->getFirstErrors())[0<span>]; </span><span>66</span><span> } </span><span>67</span><span>return</span><span>$model</span><span>; </span><span>68</span><span> } </span><span>69</span><span>70</span><span>public</span><span>function</span> actionDelete(<span>$id</span><span>) </span><span>71</span><span> { </span><span>72</span><span>return</span><span>$this</span>->findModel(<span>$id</span>)-><span>delete(); </span><span>73</span><span> } </span><span>74</span><span>75</span><span>public</span><span>function</span> actionView(<span>$id</span><span>) </span><span>76</span><span> { </span><span>77</span><span>return</span><span>$this</span>->findModel(<span>$id</span><span>); </span><span>78</span><span> } </span><span>79</span><span>80</span><span>protected</span><span>function</span> findModel(<span>$id</span><span>) </span><span>81</span><span> { </span><span>82</span><span>$modelClass</span> = <span>$this</span>-><span>modelClass; </span><span>83</span><span>if</span> ((<span>$model</span> = <span>$modelClass</span>::findOne(<span>$id</span>)) !== <span>null</span><span>) { </span><span>84</span><span>return</span><span>$model</span><span>; </span><span>85</span> } <span>else</span><span> { </span><span>86</span><span>throw</span><span>new</span> NotFoundHttpException('The requested page does not exist.'<span>); </span><span>87</span><span> } </span><span>88</span><span> } </span><span>89</span><span>90</span><span>public</span><span>function</span> checkAccess(<span>$action</span>, <span>$model</span> = <span>null</span>, <span>$params</span> =<span> []) </span><span>91</span><span> { </span><span>92</span><span>//</span><span> 检查用户能否访问 $action 和 $model </span><span>93</span><span> // 访问被拒绝应抛出ForbiddenHttpException </span><span>94</span><span> // var_dump($params);exit;</span><span>95</span><span> } </span><span>96</span> }SiteController.php
Please create the controller under the modules/controllers folder and pay attention to the namespace Modifications.
It should be noted that the controller here is different from the ordinary controller that inherits Controller. It needs to inherit the ActiveController class.
The 20~30 lines of commented code are based on the access_token authentication of the RESTful architecture. It has not been tested yet and will be added later.
At this point, all configurations based on YII2 have been basically completed. Next, we will introduce the api interface testing tools and methods.
RESTful testing tool PostMAN:
First, let’s introduce the postman plug-in, which is a practical plug-in based on Google Chrome that simulates requests. The specific use involves screenshots during the following test process. Please forgive me for any shortcomings in the introduction. This is my first time using it.
It is recommended to use the above APP version to facilitate subsequent packaging of the api interface written by yourself. The following is the web version.
YII2 supports four RESTful request methods: GET to view information, POST to create information, PUT to update information, and DELETE to delete information.
The following is a demonstration of four ways to request data. (This is just a screenshot to demonstrate the effect. You still need to explore the specific use by yourself.)
What is demonstrated here is the GET method to request database data. Corresponding to the modules/controllers/SiteController/actionIndex method.
Please pay attention to the URL address in the top box. By default, REST will make plural requests to the controller http://api.mxq.com/v1/sites. Here are the default rules of REST.
The positions marked with * show normal results. If an error occurs, you can go to YII Authoritative Guide - Error to check the cause of the error.
YII2’s ActiveController implements data paging effect by default.
What is demonstrated here is the data of the new database created by the POST method. Corresponding to the modules/controllers/SiteController/actionCreate method.
If you write data verification rules in the data layer of the database, corresponding errors will be displayed when the data submitted here does not meet the requirements. This is also one of the advantages of REST. For example, in the following situation, I define the flag as int type:
The next demonstration is the PUT method to update the data in the database. Corresponding to the modules/controllers/SiteController/actionUpdate method.
Please pay attention to the top URL again: http://api.mxq.com/v1/sites/15 Here 15 represents the data with database ID 15, which means updating the database ID to 15 data information. Please pay attention. When RESTful uses update and delete data operations, the id cannot be submitted in the form of a form and must follow the URL.
The next demonstration is the DELETE method to delete data from the database. Corresponding to the modules/controllers/SiteController/actionDelete method.
When the return value is 1, it means that the deletion operation was successfully executed. For the specific principle, please carefully observe the functions in the sitecontroller controller.
The above is some brief introduction, implementation methods and test results of RESTful based on yii2. If there are any errors or omissions, please feel free to add them. Subsequent updates will be made on this basis. This is my first time coming into contact with the yii2 framework and RESTful architecture. Please forgive me if there are any mistakes in my description.
The above introduces the configuration, implementation, and testing of Yii2 advanced version API interface development based on RESTful architecture, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

许多用户在选择智能手表的时候都会选择的华为的品牌,其中华为GT3pro和GT4都是非常热门的选择,不少用户都很好奇华为GT3pro和GT4有什么区别,下面就就给大家介绍一下二者。华为GT3pro和GT4有什么区别一、外观GT4:46mm和41mm,材质是玻璃表镜+不锈钢机身+高分纤维后壳。GT3pro:46.6mm和42.9mm,材质是蓝宝石玻璃表镜+钛金属机身/陶瓷机身+陶瓷后壳二、健康GT4:采用最新的华为Truseen5.5+算法,结果会更加的精准。GT3pro:多了ECG心电图和血管及安

为什么截图工具在Windows11上不起作用了解问题的根本原因有助于找到正确的解决方案。以下是截图工具可能无法正常工作的主要原因:对焦助手已打开:这可以防止截图工具打开。应用程序损坏:如果截图工具在启动时崩溃,则可能已损坏。过时的图形驱动程序:不兼容的驱动程序可能会干扰截图工具。来自其他应用程序的干扰:其他正在运行的应用程序可能与截图工具冲突。证书已过期:升级过程中的错误可能会导致此issu简单的解决方案这些适合大多数用户,不需要任何特殊的技术知识。1.更新窗口和Microsoft应用商店应用程

第1部分:初始故障排除步骤检查苹果的系统状态:在深入研究复杂的解决方案之前,让我们从基础知识开始。问题可能不在于您的设备;苹果的服务器可能会关闭。访问Apple的系统状态页面,查看AppStore是否正常工作。如果有问题,您所能做的就是等待Apple修复它。检查您的互联网连接:确保您拥有稳定的互联网连接,因为“无法连接到AppStore”问题有时可归因于连接不良。尝试在Wi-Fi和移动数据之间切换或重置网络设置(“常规”>“重置”>“重置网络设置”>设置)。更新您的iOS版本:

在许多中心化交易所出现问题后,越来越多的币圈投资者开始将资产转移到冷钱包中,以减少中心化交易所带来的风险。本文将介绍全球最早的冷钱包供应商Trezor,自2014年推出首款冷钱包至今,在全球多个国家销售。Trezor的产品包括2014年推出的ModelOne和2018年推出的进阶版本ModelT。下面将继续介绍这两款产品与其他冷钱包的区别。什么是Trezor冷钱包?2014年,Trezor推出了第一款冷钱包ModelOne。除了常见的BTC、ETH、USDT等币种外,该钱包还支持超过1000种其

随着互联网的不断发展,Web应用程序开发的需求也越来越高。对于开发人员而言,开发应用程序需要一个稳定、高效、强大的框架,这样可以提高开发效率。Yii是一款领先的高性能PHP框架,它提供了丰富的特性和良好的性能。Yii3是Yii框架的下一代版本,它在Yii2的基础上进一步优化了性能和代码质量。在这篇文章中,我们将介绍如何使用Yii3框架来开发PHP应用程序。

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code<form name="myform"

随着云计算技术的不断发展,数据的备份已经成为了每个企业必须要做的事情。在这样的背景下,开发一款高可用的云备份系统尤为重要。而PHP框架Yii是一款功能强大的框架,可以帮助开发者快速构建高性能的Web应用程序。下面将介绍如何使用Yii框架开发一款高可用的云备份系统。设计数据库模型在Yii框架中,数据库模型是非常重要的一部分。因为数据备份系统需要用到很多的表和关

在当前信息时代,大数据、人工智能、云计算等技术已经成为了各大企业关注的热点。在这些技术中,显卡渲染技术作为一种高性能图形处理技术,受到了越来越多的关注。显卡渲染技术被广泛应用于游戏开发、影视特效、工程建模等领域。而对于开发者来说,选择一个适合自己项目的框架,是一个非常重要的决策。在当前的语言中,PHP是一种颇具活力的语言,一些优秀的PHP框架如Yii2、Ph


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
