使用CakePHP创建API
我有一个简单的CakePHP应用程序,允许用户创建和编辑帖子。我希望在将来的某个时候将应用程序引入PhoneGap 因此,我创建了一个API,它吐出JSON用于AJAX请求,但我感觉我做得不对,因为我没有使用REST或做任何不同的事情,使它与控制器中的其他代码不同 e、 g.(注意:在本例中,我缺少将其转换为JSON的部分) 创建一个url,如:使用CakePHP创建API,php,cakephp,Php,Cakephp,我有一个简单的CakePHP应用程序,允许用户创建和编辑帖子。我希望在将来的某个时候将应用程序引入PhoneGap 因此,我创建了一个API,它吐出JSON用于AJAX请求,但我感觉我做得不对,因为我没有使用REST或做任何不同的事情,使它与控制器中的其他代码不同 e、 g.(注意:在本例中,我缺少将其转换为JSON的部分) 创建一个url,如:domain.com/api/posts/all(将创建自定义路由以实现此目的),然后我可以使用AJAX调用该url以在我的移动应用程序中使用 现在我的
domain.com/api/posts/all
(将创建自定义路由以实现此目的),然后我可以使用AJAX调用该url以在我的移动应用程序中使用
现在我的问题是,使用REST会有什么不同?我是构建应用程序的新手,我的优势在于前端开发,而不是后端开发,因此非常感谢您提供的任何帮助。在CakePHP中启用REST基本上将正确的HTTP方法路由到操作。因此,GET请求将路由到索引或视图操作,DELETE请求路由到DELETE操作,依此类推 这为使用API的用户创建了一个非常简单的端点。然后,在调用此端点时,根据HTTP方法,Cake会将其路由到正确的操作(请原谅任何HTTP请求语法错误): 路由到/posts/index.json的GET请求
GET /api/posts.json HTTP/1.1
Host: example.com
路由到/posts/edit/1.json的POST请求
POST /api/posts/1.json HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Content-Length: 24
data[Post][name]=updated
阅读这篇文章将回答你的大部分问题:如果你想对其他校长说实话的话 然后,通常需要记住4点:
- web服务的基本URI
- web服务支持的数据的Internet媒体类型。
这通常是JSON、XML或YAML,但也可以是任何其他有效的Internet 媒体类型 - web服务使用HTTP方法支持的操作集 (例如,获取、放置、发布或删除)
- API必须是超文本驱动的
内容类型:application/json
请求Url:
请求方法:获取
您的应用程序应该能够处理这种类型的请求,而不需要在URI中规定操作,这让我们回到第(1)点 而不是以这种方式编写URI: domain.com/api/posts/all 您的URI应该是这样建模的:
resources/posts/12作为resources/type/item从集合中检索一个成员,
资源/发布为使用整个集合的资源/类型 下面是一个代码示例: 公共抽象类 在这里,您可以实现一些常见任务。 如果是,则使用基于服务的实现 这也可以通过服务来完成
abstract class ResourcesController extends AppController {
}
class PostResourcesController extends ResourcesController {
/**
* By the time this method is called in your controller/class, you already know
* that the HTTP method is GET.
*
* @param Request\$_GET $request A request instance
* @param int $postId The post ID to retrieve
*
* @return Response A reponse instance
*/
function getPost(Request $Request, $postId = null)
{
/**
* Here you can use the request object to get
* the response content type
* the requesting client accepts. Example JSON or XML.
*/
/**
* using the $postId you can then query your database
* to retrieve a post with that ID or use a sort of
* service.
*/
/**
* Once you implemented a you logic
* you can build a response to return.
*/
}
}
这个代码是不完整的,但我希望它能
您可以了解真正的Restful API是什么样子的
该键用于确保“应用程序可以通过了解两件事与资源交互:资源的标识符和所需的操作”
希望这会有所帮助。好吧,我已经说过了。。。它与只在控制器中创建标准方法而不使用REST有何不同?另外,这似乎是一个不同的问题,但是DELETE方法在表单的POST或GET方法中提供了什么呢?首先,它对开发人员和您来说都更容易(创建文档)。更少的端点。其次,它需要适当的HTTP方法。例如,用户不再能够在浏览器中访问
/api/posts/delete/1
并删除帖子,他们实际上必须使用适当的方法请求删除。此外,Cake的REST系统将允许您接受不同的内容类型,并在$this->request->data
中为您自动解码。如果它不适用于您,则无需这样做,但这是创建RESTful API的正确方法。如果您在PhoneGab应用程序中使用Sencha Touch,也可以使用而不是RESTful。
POST /api/posts/1.json HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Content-Length: 24
data[Post][name]=updated
abstract class ResourcesController extends AppController {
}
class PostResourcesController extends ResourcesController {
/**
* By the time this method is called in your controller/class, you already know
* that the HTTP method is GET.
*
* @param Request\$_GET $request A request instance
* @param int $postId The post ID to retrieve
*
* @return Response A reponse instance
*/
function getPost(Request $Request, $postId = null)
{
/**
* Here you can use the request object to get
* the response content type
* the requesting client accepts. Example JSON or XML.
*/
/**
* using the $postId you can then query your database
* to retrieve a post with that ID or use a sort of
* service.
*/
/**
* Once you implemented a you logic
* you can build a response to return.
*/
}
}