Yii2¿ò¼ÜÖÆ×÷RESTful·ç¸ñµÄAPI¿ìËÙÈëÃŽ̳Ì

5年以前  |  阅读数:418 次  |  编程语言:PHP 

Ïȸø´o¼Ò˵ÏÂʲôÊÇREST

restful

RESTÈ«³ÆÊÇRepresentational State Transfer£¬ÖÐÎÄÒa˼ÊDZiÊo£¨±aÕß×¢£ºÍ¨³£ÒeΪ±iÕ÷£(C)ÐÔ״̬תÒÆ¡£ ËuÊ״γoÏÖÔÚ2000ÄeRoy FieldingµÄ²(C)Ê¿ÂÛÎÄÖУ¬Roy FieldingÊÇHTTP¹ae¶µÄÖ÷Òª±aдÕßÖ®Ò»¡£ ËuÔÚÂÛÎÄÖÐÌaµ½£º"ÎÒÕaƪÎÄÕµÄд×÷Ä¿µÄ£¬¾ÍÊÇÏeÔÚuºÏ¼Ü¹¹Ô­ÀiµÄÇ°ÌaÏ£¬Ài½aºÍÆÀ¹ÀÒÔÍøÂçΪ»u´¡µÄÓ¦ÓÃÈi¼þµÄ¼Ü¹¹Ée¼Æ£¬µÃµ½Ò»¸o¹¦ÄÜÇ¿¡¢ÐÔÄܺá¢ÊÊÒËͨÐŵļܹ¹¡£RESTÖ¸µÄÊÇÒ»×e¼Ü¹¹Ô¼ÊøÌo¼þºÍÔ­Ôo¡£" Èç¹uÒ»¸o¼Ü¹¹*uºÏRESTµÄÔ¼ÊøÌo¼þºÍÔ­Ôo£¬ÎÒÃǾͳÆËuΪRESTful¼Ü¹¹¡£

REST±¾Éi²¢Ã»Óд´Ôiеļ¼Êo¡¢×e¼þ»oþÎñ£¬¶øÒþ²ØÔÚRESTful±³ºoµÄÀiÄi¾ÍÊÇʹÓÃWebµÄÏÖÓÐÌØÕ÷ºÍÄÜÁ¦£¬ ¸uºÃµØʹÓÃÏÖÓÐWeb±e×¼ÖеÄÒ»Ð(C)×¼ÔoºÍÔ¼Êø¡£ËaÈ»REST±¾ÉiÊÜWeb¼¼ÊoµÄÓ°ÏiºÜÉi£¬ µ«ÊÇÀiÂÛÉÏREST¼Ü¹¹ç¸ñ²¢²»ÊÇ°o¶¨ÔÚHTTPÉÏ£¬Ö»²»¹ýÄ¿Ç°HTTPÊÇΨһÓeRESTÏa¹ØµÄʵÀý¡£ ËuÒÔÎÒÃÇÕaÀiÃeÊoµÄRESTÒ²ÊÇͨ¹ýHTTPʵÏÖµÄREST¡£

Ç°²»¾Ã×oÒ»¸oÏiÄ¿£¬ÊÇÓÃYii2¿o¼ÜдһÌ×RESTfulç¸ñµÄAPI£¬¾ÍÈ¥²eÁËÏ¡¶Yii 2.0 ȨÍþÖ¸ÄÏ ¡£¬*¢ÏÖÉÏÃaeдµÃ±È½Ï¼oÂÔ¡£ËuÒÔ¾ÍÔÚÕaÀiдһƪ½Ì³ÌÌu£¬Ï£Íu°iÖu¸Õ½Ó´¥Yii2¿o¼ÜRESTfulµÄС»i°e¿iËÙÈeÃÅ¡£

Ò»¡¢Ä¿Â¼½a¹¹

ʵÏÖÒ»¸o¼oµ¥µØRESTful APIÖ»ÐeÓõ½Èý¸oÎļþ¡£Ä¿Â¼ÈçÏ£º


    frontend
    (C)À(C)¤ config
    (C)¦ (C)¸ main.php
    (C)À(C)¤ controllers
    (C)¦ (C)¸ BookController.php
    (C)¸(C)¤ models
    (C)¸ Book.php

¶þ¡¢ÅaÖÃURL¹aeÔo

1.ÐÞ¸Ä*þÎñÆ÷µÄrewrite¹aeÔo£¬½«ËuÓÐURLÈ«²¿Ö¸Ïoindex.phpÉÏ£¬Ê¹ÆaÖ§³Ö /books/1 ¸ñʽ¡£

Èç¹uÊÇApache*þÎñÆ÷£¬ÔÚfrontend/web/ Ŀ¼ÖÐн¨.htaccessÎļþ¡£ÎļþÄÚÈÝÈçÏ£º


    RewriteEngine on
    # If a directory or a file exists, use the request directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # Otherwise forward the request to index.php
    RewriteRule . index.php

Èç¹uÊÇNginx*þÎñÆ÷£¬ÐÞ¸Änginx/conf/nginx.conf£¬ÔÚµ±Ç°"server{}"µÄ"location / {}"ÖÐÌi¼ÓÏÂÃaeºiÉ«±e¼ÇÄÚÈÝ£º


    location / {
    ¡¡¡¡try_files $uri $uri/ /index.php$is_args$args;
    }

2.ÐÞ¸Äfrontend/config/main.phpÎļþ£¬Îªbook¿ØÖÆÆ÷Ôo¼ÓÒ»¸o URL ¹aeÔo¡£ÕaÑu£¬¾ÍÄÜͨ¹ýÃÀ»¯µÄ URL ºÍÓÐÒaÒaµÄ http ¶¯´Ê½øÐÐ*ÃÎʺͲÙ×÷Êý¾Ý¡£ÅaÖÃÈçÏ£º


    'components' => [
    'urlManager' => [
    'enablePrettyUrl' => true,
    'enableStrictParsing' => true,
    'showScriptName' => false,
    'rules' => [
    ['class' => 'yii\rest\UrlRule', 'controller' => 'book'],
    ],
    ],
    ],

Èý¡¢´´½¨Ò»¸omodel

1.ÔÚÊý¾Ý¿aÖд´½¨Ò»ÕÅbook±i¡£book±iµÄÄÚÈÝÈçÏ£º


    -- ----------------------------
    -- Table structure for book
    -- ----------------------------
    DROP TABLE IF EXISTS `book`;
    CREATE TABLE `book` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `name` char(50) NOT NULL DEFAULT '',
    `num` tinyint(3) unsigned NOT NULL DEFAULT '0',
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
    -- ----------------------------
    -- Records of book
    -- ----------------------------
    INSERT INTO `book` VALUES ('1', 'toefl', '10');
    INSERT INTO `book` VALUES ('2', 'ielts', '20');
    INSERT INTO `book` VALUES ('3', 'sat', '30');
    INSERT INTO `book` VALUES ('4', 'gre', '40');
    INSERT INTO `book` VALUES ('5', 'gmat', '50');

2.ÔÚfrontend/models/Ŀ¼ÖÐн¨Book.php¡£ÎļþÄÚÈÝÈçÏ£º


    namespace frontend\models;
    use yii\db\ActiveRecord;
    class Book extends ActiveRecord
    {
    public static function tableName()
    {
    return 'book';
    }
    }

ËÄ¡¢´´½¨Ò»¸o¿ØÖÆÆ÷

ÔÚfrontend/controllers/Ŀ¼ÖÐн¨BookController.php¡£¿ØÖÆÆ÷ÀaÀ(C)Õ¹×Ô yii\rest\ActiveController¡£Í¨¹ýÖ¸¶¨ yii\rest\ActiveController::modelClass ×÷Ϊ frontend\models\Book£¬ ¿ØÖÆÆ÷¾ÍÄÜÖªµÀʹÓÃÄĸoÄ£ÐÍÈ¥»ñÈ¡ºÍ´¦ÀiÊý¾Ý¡£ÎļþÄÚÈÝÈçÏ£º


    namespace frontend\controllers;
    use yii\rest\ActiveController;
    class BookController extends ActiveController
    {
    public $modelClass = 'frontend\models\Book';
    }

Îa¡¢²aÊÔ

µ½ÕaÀi£¬ÎÒÃǾÍÒѾ­Íe³ÉÁË´´½¨ÓÃÓÚÃÎÊÓû§Êý¾Ý µÄ RESTful ç¸ñµÄ API¡£´´½¨µÄ API °uÀ¨£º

GET /books: ÁгoËuÓеÄÊe
HEAD /books: ÏÔʾÊeµÄÁбiµÄ¸ÅÒªÐÅÏ¢
POST /books: ÐÂÔo1±¾Êe
GET /books/1: *µ»Ø ÊeID=1µÄÏeϸÐÅÏ¢
HEAD /books/1: ÏÔʾ ÊeID=1µÄ¸ÅÊoÐÅÏ¢
PATCH /books/1 and PUT /books/1: ¸uÐÂÊeID=1µÄÐÅÏ¢
DELETE /books/1: ɾ³ýÊeID=1µÄÐÅÏ¢
OPTIONS /books: ÏÔʾ¹ØÓÚÄ(C)¶Ë /books Ö§³ÖµÄ¶¯´Ê
OPTIONS /books/1: ÏÔʾÓйØÄ(C)¶Ë /books/1 Ö§³ÖµÄ¶¯´Ê

¿ÉÒÔͨ¹ýWeba¯ÀÀÆ÷ÖÐÊaÈe URL http://{frontendµÄÓoÃu}/books À´ÃÎÊAPI£¬»oÕßʹÓÃÒ»Ð(C)a¯ÀÀÆ÷²a¼þÀ´¢ËÍÌض¨µÄ headers ÇeÇo£¬±ÈÈçFirefoxµÄRestClient¡¢ChromeµÄAdvanced Rest Client¡¢postmanµÈ¡£

Áu¡¢ËµÃ÷

1.Yii ½«ÔÚÄ(C)¶ËʹÓõĿØÖÆÆ÷µÄÃu³Æ×Ô¶¯±aΪ¸´Êý¡£ÕaÊÇÒoΪ yii\rest\UrlRule Äܹ»ÎªËuÃÇʹÓõÄÄ(C)¶ËÈ«×Ô¶¯¸´Êý»¯¿ØÖÆÆ÷¡£¿ÉÒÔͨ¹ýÉeÖÃyii\rest\UrlRule::pluralizeΪfalseÀ´½uÓôËÐÐΪ:


    'rules' => [
    ['class' => 'yii\rest\UrlRule', 'controller' => 'book', 'pluralize' => false],
    ],

2.¿ÉÒÔʹÓÃfieldsºÍexpand²ÎÊýÖ¸¶¨ÄÄÐ(C)×Ö¶ÎÓ¦¸Ã°uº¬ÔÚ½a¹uÄÚ¡£ÀýÈ磺URL http://{frontendµÄÓoÃu}/books?fields=name,num ½«Ö»*µ»Ø name ºÍ num ×ֶΡ£

ÒÔÉÏËuÊoÊÇС±a¸ø´o¼Ò½eÉܵÄYii2¿o¼ÜÖÆ×÷RESTfulç¸ñµÄAPI¿iËÙÈeÃŽ̳̣¬Ï£Íu¶Ô´o¼ÒÓÐËu°iÖu£¬Èç¹u´o¼ÒÓÐÈκÎÒÉÎÊÇe¸øÎÒÁoÑÔ£¬Ð¡±a»a¼°Ê±»Ø¸´´o¼ÒµÄ¡£ÔÚ´ËҲdz£¸Ðл´o¼Ò¶Ô½Å±¾Ö®¼ÒÍøÕ¾µÄÖ§³Ö£¡

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例