1. Go to this page and download the library: Download daothink/think-api 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/ */
daothink / think-api example snippets
$api = new \Zewail\Api\Routing\Router;
$api->version('v1', function () {
// TODO 可以是thinkphp自带的路由
});
use Zewail\Api\Facades\ApiRoute;
ApiRoute::version('v1', function(){
// TODO 可以是thinkphp自带的路由
});
ApiRoute::version(['v1', 'v2'], function () {
// TODO 可以是thinkphp自带的路由
});
ApiRoute::version('v1', function () {
Route::rule('new/:id','app\index\controller\V2\News@read');
// 或者
Route::rule('new/:id','index/V2.News/read');
});
ApiRoute::version('v2', function () {
Route::rule('new/:id','app\index\controller\V2\News@read');
});
namespace app\index\controller;
use think\Controller;
use Zewail\Api\Api;
class BaseController extends Controller
{
use Api;
}
namespace app\index\controller;
use Zewail\Api\Facades\Response as ApiResponse;
class IndexController
{
public function index() {
return ApiResponse::array([]);
}
}
namespace app\index\controller;
use app\index\model\User;
use Zewail\Api\Facades\Response;
use Zewail\Api\Facades\JWT;
use Zewail\Api\Exceptions\JWTException;
class Authenticate
{
public function authenticate()
{
// $credentials 可以从请求中获取
$credentials = ['email'=>'[email protected]', 'password' => '123456'];
$token = JWT::attempt($credentials);
}
}
namespace app\index\model;
use think\Model;
class User extends Model
{
public $jwtSub = 'mobile';
}
public $jwtPassword = 'strange_password';
public function jwtEncrypt($password)
{
// 只要返回你加密后的结果,会自动比对数据库字段
return md5(sha1($password));
}