使用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个回答



您应该在 GET / wines / </ code>的响应中添加一个表单,其中描述了 POST / wine / </ code>你应该为每个 GET /wines/:id-s.


nn

添加链接你可以使用 HAL 链接,以及一些 ad-hoc解决方案。 如果您需要更复杂的解决方案,可以将 Hydra 与JSON-LD一起使用。</ p> \ n </ div>

展开原文

原文

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.

douyun1950
douyun1950 谢谢(你的)信息。
接近 6 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐