1

我正在尝试使用 Parse 作为云数据库在 Symfony2 中创建 API Rest。

如果我尝试检索 Parse 用户,它可以正常工作并返回预期的数据。

本地网址示例:http://www.foo.local/app_dev.php/getUsers/

这是我在用户控制器中使用的代码(我使用注释来设置控制器中的路由):

namespace Foo\ApiBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations\View;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Parse\ParseClient;
use Parse\ParseObject;
use Parse\ParseQuery;
use Parse\ParseUser;

class UsersController extends Controller
{
    /**
    * @return array
    * @View()
    * @Route("/getUsers/")
    */
    public function getUsersAction(Request $request) {
        ParseClient::initialize(<my Parse keys>);

        $query = ParseUser::query();     
        $results = $query->find();

        return array('users' => $results);
    } 
}

但是,如果我对我的 Products ParseObjects 尝试相同的操作,我会收到以下错误消息:

error code="500" message="Internal Server Error" exception class="Doctrine\Common\Annotations\AnnotationException" message="[Semantical Error] Parse\ParseFile::getData() 方法中的注释“@returns”从未已导入。您是否可能忘记为此注释添加“使用”语句?

本地网址示例:http://www.foo.local/app_dev.php/getProducts/

产品控制器代码:

namespace Foo\ApiBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations\View;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Parse\ParseClient;
use Parse\ParseObject;
use Parse\ParseQuery;
use Parse\ParseUser;
use Parse\ParseFile;

class ProductsController extends Controller 
{
    /**
     * @return array
     * @View()
     * @Route("/getProducts/")
     */
    public function getProductsAction(Request $request) {
        ParseClient::initialize(<my Parse keys>);

        $query = new ParseQuery("Products");
        $results = $query->find();

        return array('products' => $results);    
    }
}

如果我没有返回,而是$results返回其他虚拟数据,例如return array('products' => 'fooProducts'),我不再收到错误消息。

此外,如果我创建一个var_dump变量$results,我会得到预期的 ParseObjects 数组。

这是我的 routing.yml 文件,以防它出现问题:

api:
    resource: "@FooApiBundle/Controller/"
    type:     annotation
    prefix:   /

users:
    type: rest
    resource: Foo\ApiBundle\Controller\UsersController

products:
    type: rest
    resource: Foo\ApiBundle\Controller\ProductsController

根据错误消息,问题似乎与 Doctrine 有关,但由于我没有使用它,我不知道究竟是如何发生冲突或如何解决它。有什么建议么?

4

2 回答 2

1

@returns类中有一些 DocBlock 拼写错误Parse\ParseFile导致 Doctrine 的 Annotations 类尝试将它们识别为一个类。这不是你的错,而是 Parse PHP SDK 库中的一个错误。

我已在此提交中进行了修复并向原始开发人员提交了一个拉取请求,因此最终运行composer update将您的 Parse 库带到最新的正确版本应该是一件简单的事情。

您可以在此处阅读有关 DocBlock 和注释部分的更多信息

这是生成的差异的复制/粘贴src/Parse/ParseFile.php

@@ -31,7 +31,7 @@ class ParseFile implements \Parse\Internal\Encodable
   /**
    * Return the data for the file, downloading it if not already present.
    *
-   * @returns mixed
+   * @return mixed
    *
    * @throws ParseException
    */
 @@ -50,7 +50,7 @@ public function getData()
   /**
    * Return the URL for the file, if saved.
    *
-   * @returns string|null
+   * @return string|null
    */
   public function getURL()
   {
 @@ -112,7 +112,7 @@ public function getMimeType()
    * @param string $name The file name on Parse, can be used to detect mimeType
    * @param string $mimeType Optional, The mime-type to use when saving the file
    *
-   * @returns ParseFile
+   * @return ParseFile
    */
   public static function createFromData($contents, $name, $mimeType = null)
   {
 @@ -132,7 +132,7 @@ public static function createFromData($contents, $name, $mimeType = null)
    * @param string $name Filename to use on Parse, can be used to detect mimeType
    * @param string $mimeType Optional, The mime-type to use when saving the file
    *
-   * @returns ParseFile
+   * @return ParseFile
    */
   public static function createFromFile($path, $name, $mimeType = null)
   {
于 2014-10-23T20:49:03.923 回答
0

使用 Symfony 初始化 Parse 的正确方法是在控制器的 setContainer 方法上:

class BaseController extends Controller
{
    ....

    public function setContainer(ContainerInterface $container = null)
    {
        parent::setContainer( $container );
        ParseClient::initialize( $app_id, $rest_key, $master_key );
    }
}

根据您的需要,您可以创建一个 BaseController 并在其余控制器中扩展它。

class UsersController extends Controller 

此外,您可以在 parameters.yml 文件中添加您的密钥。

parameters:
    #your parameters...
    ParseAppId: your_id
    ParseRestKey: your_rest_key
    ParseMasterKey: your_master_key

提示:请注意,您可以添加不同的 Parse 项目(开发版和发布版)。在不同的参数配置中添加参数提供了一种处理此问题的简单方法。

class BaseController extends Controller
{
    ....

    public function setContainer(ContainerInterface $container = null)
    {
        parent::setContainer( $container );
        $app_id = $container->getParameter('ParseAppId');
        $rest_key = $container->getParameter('ParseRestKey');
        $master_key = $container->getParameter('ParseMasterKey');
        ParseClient::initialize( $app_id, $rest_key, $master_key );
    }
}
于 2016-04-01T15:03:04.893 回答