RESTful
使用 IMI 提供的路由请求方法判断,可以实现 RESTful 风格的 api 开发。
RESTful 风格控制器示例 (query/find/create/update/delete):
<?php
namespace ImiDemo\HttpDemo\MainServer\Controller;
use Imi\Controller\HttpController;
use Imi\Server\View\Annotation\View;
use Imi\Server\Route\Annotation\Route;
use Imi\Server\Route\Annotation\Action;
use Imi\Server\Route\Annotation\Controller;
/**
* @Controller(prefix="/Rest/")
* @View(renderType="json")
*/
class Rest extends HttpController
{
/**
* query
*
* @Action
* @Route(url="", method={"GET"})
* @return void
*/
public function query()
{
return [1, 2, 3];
}
/**
* find
*
* @Action
* @Route(url="{id}", method={"GET"})
*
* @param int $id
* @return void
*/
public function find($id)
{
return [
'id' => $id,
];
}
/**
* create
*
* @Action
* @Route(url="", method={"POST"})
* @return void
*/
public function create()
{
return [
'operation' => 'create',
'postData' => $this->request->getParsedBody(),
'success' => true,
];
}
/**
* update
*
* @Action
* @Route(url="{id}", method={"PUT"})
*
* @param int $id
* @return void
*/
public function update($id)
{
return [
'id' => $id,
'operation' => 'update',
'success' => true,
];
}
/**
* delete
*
* @Action
* @Route(url="{id}", method={"DELETE"})
*
* @param int $id
* @return void
*/
public function delete($id)
{
return [
'id' => $id,
'operation' => 'delete',
'success' => true,
];
}
}
当前内容版权归 imiphp.com 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 imiphp.com .