简体   繁体   English

最佳实践,以及如何在Symfony2中从iOS AFNetworking中获取POST数据并在GET中返回JSON?

[英]Best Practices and how to find get POST data from iOS AFNetworking in Symfony2 and return JSON in GET?

I am building a mobile app (iOS) and Symfony2 REST backend. 我正在构建一个移动应用程序(iOS)和Symfony2 REST后端。 On Symfony2, my routes are working correctly and I have tested them with AJAX and httpie, all CRUD operations, etc are fine. 在Symfony2上,我的路由正常运行,并且已经使用AJAX和httpie测试了它们,所有CRUD操作等等都很好。 Now, I am trying to access the routes from the app. 现在,我正在尝试从该应用访问路线。 So far, I can access the routes and when I look into the Symfony2 Profiler, I can see entries in last 10 entries to verify that I am hitting the server with my POST and GET requests. 到目前为止,我可以访问路由,当我查看Symfony2 Profiler时,可以看到最近10个条目中的条目,以验证我是否通过POST和GET请求访问了服务器。 Now, I have 2 questions and I would be glad if people can point me in the direction for ** Best Practices ** on how to proceed. 现在,我有2个问题,如果有人能指出我的“最佳做法”指导,我将非常高兴。

Problem 1: Although I am posting data which I can see coming in under "Request", when I try to create a record, it creates only NULL records, meaning the data is being lost. 问题1:尽管我发布的数据可以在“请求”下看到,但是当我尝试创建记录时,它仅创建NULL记录,这意味着数据正在丢失。 This is my controller for creating users for example: 例如,这是我用于创建用户的控制器:

public function postUserAction(Request $request)
{
    $content = $this->get('request')->getContent();
    $serializer = $this->get('jms_serializer');
    $entity = $serializer->deserialize($content, 'Name\BundleName\Entity\User', 'json');

        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

    return array(
        'entity' => $entity,
    );
}

When I look into the log, the only things that stand out are: Request Cookies (No cookies), Request Content: "Request content not available (it was retrieved as a resource)." 当我查看日志时,唯一突出的是:请求Cookie(无cookie),请求内容:“请求内容不可用(已作为资源检索)。” This tells me the data was missing, how can I get this data and use it? 这告诉我数据丢失了,如何获取和使用数据? Or what else could it be? 还是什么呢?

Problem 2: GET returns an empty JSON response with no data just the keys when I NSlog (echo it). 问题2:当我执行NSlog(回显它)时,GET返回一个空的JSON响应,其中不包含任何数据。 My code looks like: 我的代码如下:

public function getUsersAction()
{
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository('NameBundle:User')->findAll();

    return array(
        'entities' => $entities,
    );
}

From the log, it has the Request Cookies set: PHPSESSID => "1udududjjs83883jdlb4ho0j4" but again the Request Content says: "Request content not available (it was retrieved as a resource)." 从日志中,它设置了“请求Cookie”:PHPSESSID =>“ 1udududjjs83883jdlb4ho0j4”,但是请求内容再次显示:“请求内容不可用(已将其作为资源检索)。” How can I make it return the data with the JSON? 如何使它使用JSON返回数据? This works well in the browser AJAX and httpie tests. 这在浏览器AJAX和httpie测试中效果很好。

Problem 3: Using AFNetworking, I have a symbolic constant which I set as the APIHost (IP Address) and APIPath was the folder. 问题3:使用AFNetworking,我有一个符号常量,将其设置为APIHost(IP地址),而APIPath是文件夹。 Now in my earlier version using native PHP, I constructed the actual code to be executed in index.php by sending the parameter in JSON so if I wanted a login, I sent something like todo:login but with Symfony2, I am not sure or know even the best practices for this case. 现在,在使用本地PHP的早期版本中,我通过在JSON中发送参数来构造要在index.php中执行的实际代码,因此,如果要登录,我发送了类似todo:login的内容,但使用Symfony2,我不确定还是甚至不知道这种情况的最佳做法。 Ideally, I would like to specify the server-side request in the JSON request and then find the correct route in Symfony2 but is this how to do it and if yes, can you please provide an example? 理想情况下,我想在JSON请求中指定服务器端请求,然后在Symfony2中找到正确的路由,但这是怎么做的,如果可以,请提供示例吗? The workaround is to specify hard coded paths in AFNetworking each time I need to make a request which I think tightly couples the code and I need to make changes in a lot of places anytime something changes on the server side. 解决方法是每次我需要发出请求时都在AFNetworking中指定硬编码路径,我认为该请求将代码紧密耦合在一起,并且每次服务器端发生任何更改时都需要在很多地方进行更改。 Thanks and sorry for the long question! 谢谢您,还有很长的问题很抱歉!

You expect the jmsserializer to do magic for you. 您希望jmsserializer为您做魔术。 But it won't, you have to configure it first. 但是不会,您必须先对其进行配置。 From you code I can see that you are using jmsserializer wrong. 从您的代码中,我可以看到您使用jmsserializer错误。

In getUsersAction() you have to return a serialized response, but you are returning an array of objects. getUsersAction()您必须返回一个序列化的响应,但是您将返回一个对象数组。 This would be the right way: 这是正确的方法:

public function getUsersAction()
{
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository('NameBundle:User')->findAll();
    $serializer = $container->get('jms_serializer');

    return array(
        'users' => $jsonContent = $serializer->serialize($entities, 'json');,
    );
}

Your post action basically looks ok, however when the json does not contain every field of entity USER the deserialization will fail. 您的发布操作基本上看起来还可以,但是当json不包含实体USER的每个字段时,反序列化将失败。 You can configure the entity for serialization/deserialization using annotations. 您可以使用注释配置实体以进行序列化/反序列化。

http://jmsyst.com/libs/serializer/master/reference/annotations http://jmsyst.com/libs/serializer/master/reference/annotations

I am not sure if I understood your last problem, but I think you have to hardcode the path in your app. 我不确定我是否了解您的最后一个问题,但是我认为您必须在应用程序中对路径进行硬编码。


Symfony2 is great and absolutely useful when writing an API. 在编写API时,Symfony2很棒,而且绝对有用。 But if you don't want to deal with serialization/deserialization you can give http://laravel.com/ a try. 但是,如果您不想处理序列化/反序列化,则可以尝试一下http://laravel.com/ It is build on symfony and you can generate an api on the fly. 它基于symfony构建,您可以动态生成一个api。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM