使用 REST API 从表中获取数据
我正在使用 Slim 框架设计 REST api。我正在使用数据库 mySql。我正在 php 中设计这个 API。
我正在尝试从我的表中获取有关学生的数据。
我正在尝试这样:-
<?php
header('Content-type: application/json');
// Include the Slim library
require 'Slim/Slim.php';
// Instantiate the Slim class
$app = new Slim();
// Create a GET-based route
$app->get('/hello/:name', 'hello');
function hello($name)
{
// here is code to access detail of $name
echo $name
// how can i get detail if i have value of name=:kuntal not name=kuntal
}
// Ready the routes and run the application
$app->run();
?>
我正在使用此网址尝试此功能:- 192.168.1.101/hello/:kuntal
我需要获取名称值为 kuntal 但在函数中我获取的名称值为 :kuntal 所以请告诉我如何才能删除名称前的 :(冒号)。
是另一种方法来做到这一点。
如果您了解制作 REST API 的 slim 框架,请给我您的建议。 先感谢您。
I am designing REST api using Slim framework. I am using database mySql. and i am designing this API in php.
I am trying to fetch data about student from my table.
I am trying like this:-
<?php
header('Content-type: application/json');
// Include the Slim library
require 'Slim/Slim.php';
// Instantiate the Slim class
$app = new Slim();
// Create a GET-based route
$app->get('/hello/:name', 'hello');
function hello($name)
{
// here is code to access detail of $name
echo $name
// how can i get detail if i have value of name=:kuntal not name=kuntal
}
// Ready the routes and run the application
$app->run();
?>
I am trying this function using this url:-
192.168.1.101/hello/:kuntal
i need to get value of name as kuntal but in function i am getting value of name as :kuntal so please tell me how can i remove this :(colon) before the name.
Is another way to do this.
Please give me your suggestion if you are aware about slim framework to make REST API.
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个网址:192.168.1.101/hello/kuntal
我认为这肯定适用于您的代码。
Try this url: 192.168.1.101/hello/kuntal
i think this will definitely work for your code.
Slim 是一个非常棒的框架。在我开始使用它之前,我对框架、REST、理解 HTTP 的经验为零......我仍然是一个菜鸟,但 Slim 让它变得有趣。
答案 1:
答案 2:
我可以补充一下,您可以查看 Idiorm/Paris 来满足您的数据库需求吗?与 Slim 一样,少即是多。这就是 Paris 的代码的样子。
虽然,问题(也许对 REST 了解更多的人可以回答)。那个 uri 真的好吗?根据我对 REST 的理解,我们希望为人们提供资源。我猜是名词。我不确定 Hello 在 REST 上下文中真正意味着什么。为什么不让资源用户或好友的 ID 作为 slug 呢?
然后,您可以处理该信息,将其与问候语打包在一起,无论客户期望什么。您可以将名称等额外信息传递到参数中,如下所示。
这有帮助。非常酷的东西。有人给我反馈,让我知道我的想法是否正确。我也还在学习中。
Slim is a really great framework. Before I started using it I had zero experience with frameworks, REST, understanding HTTP... I'm still a noob but Slim makes it fun.
Answer 1:
Answer 2:
Might I add that you check out Idiorm/Paris for your database needs? In the same philosophy as Slim, less is more. This is what code might look like with Paris.
Although, question (and maybe someone who knows more about REST can answer). Is that uri really a good one? From what I understand about REST, we want to point people to resources. Nouns I guess. I'm not sure what Hello would really mean in REST context. Why not make the resource user or friend with an ID as a slug?
You can then process that information, package it with a hello greeting, whatever the client is expecting. You can pass extra information like name into the parameters like so.
Hope this helps. Very cool stuff. And someone give me feedback to let me know if my thinking is on the right page. I'm still learning too.