PHP code example of jhq0113 / roach-rest-app
1. Go to this page and download the library: Download jhq0113/roach-rest-app library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jhq0113 / roach-rest-app example snippets
/**
* Created by PhpStorm.
* User: Jiang Haiqiang
* Date: 2020/7/25
* Time: 7:06 PM
*/
namespace rest\modules\v2;
/**
* Class Module
* @package rest\modules\v2
* @datetime 2020/7/25 7:07 PM
* @author roach
* @email [email protected]
*/
class Module extends \rest\base\Module
{
/**
* @var string
* @datetime 2020/7/25 7:07 PM
* @author roach
* @email [email protected]
*/
public $id = 'v2';
}
/**
* Created by PhpStorm.
* User: Jiang Haiqiang
* Date: 2020/7/25
* Time: 7:00 PM
*/
namespace rest\modules\v2\controllers;
/**
* Class Controller
* @package rest\modules\v2\controllers
* @datetime 2020/7/25 7:00 PM
* @author roach
* @email [email protected]
*/
class Controller extends \rest\base\Controller
{
/**
* @var array
* @datetime 2020/7/24 9:18 PM
* @author roach
* @email [email protected]
*/
public $actionMethodMap = [
'index' => [
'GET'
],
'info' => [
'GET'
],
'create' => [
'POST'
],
'update' => [
'PUT'
],
'delete' => [
'DELETE'
]
];
}
/**
* Created by PhpStorm.
* User: Jiang Haiqiang
* Date: 2020/7/25
* Time: 7:00 PM
*/
namespace rest\modules\v1\controllers;
/**
* Class Controller
* @package rest\modules\v1\controllers
* @datetime 2020/7/25 7:00 PM
* @author roach
* @email [email protected]
*/
class Controller extends \rest\base\Controller
{
/**
* @var array
* @datetime 2020/7/24 9:18 PM
* @author roach
* @email [email protected]
*/
public $actionMethodMap = [
//indexAction仅支持GET方式
'index' => [
'GET'
],
//infoAction支持GET和POST两种方式
'info' => [
'GET', 'POST'
],
//createAction仅支持POST方式
'create' => [
'POST'
],
//updateAction仅支持PUT方式
'update' => [
'PUT'
],
//deleteAction仅支持DELETE方式
'delete' => [
'DELETE'
],
//...未配置支持所有请求方式
];
}
return [
'components' => [
//通用异常处理
'errorHandler' => [
'class' => 'roach\exceptions\ErrorHandler',
'handler' => 'common\ErrorHandler::handler',
'calls' => [
'run'
]
]
]
];