duandushang5148 2014-12-10 12:22
浏览 31
已采纳

使用SLIM Framework创建REST API。 与REST保持一致,但提供正确的视图

I am trying to wrap my head around how to create a RESTful API with SLIM. I understand REST and I have a good understanding of slim but what I can understand is how to render the correct view templates.

This is a typical REST API

$app->get('/wines', 'getWines');
$app->get('/wines/:id',  'getWine');
$app->post('/wines', 'addWine');
$app->put('/wines/:id', 'updateWine');
$app->delete('/wines/:id',   'deleteWine');

Now what I don't understand is if all the routes have the same URL how can you render the correct view template. The first GET request displays all the wines. The next GET displays only the wine with the specified id. Now this is where my understanding falters. When I want to add a wine I write a route that reacts differently to either a GET or POST request.

$app->map('/wines', function() {
    echo "I respond to multiple HTTP methods!";
})->via('GET', 'POST'); 

So for the get request I want show a form so I can enter a new wine but as the URL is the same as getting all the wines and they are both GET requests how can I render the correct template.

I hope this makes sense.

Thanks

  • 写回答

1条回答

  • dqkkrt8860 2014-12-10 13:01
    关注

    You should add a form to the response to GET /wines/ which describes the POST /wines/ and you should add links for each of the GET /wines/:id-s.

    You can use HAL for the links, and some ad-hoc solution for the form. If you want a more sophisticated solution, you can use Hydra with JSON-LD.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题