无法在Controller中使用命名空间

时间:2016-10-28 09:27:10

标签: php symfony

我有这个控制器:

namespace FacilitaTripBundle\Controller;

use FacilitaTripBundle\Api\models\GuideDestination\GuideDestinationModel;
use FacilitaTripBundle\Api\models\Destination\DestinationModel;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;

/**
 * @Route("/api/v1", name="api")
 */
class ApiController extends Controller
{

  /**
   * @Route("/destinations/get_all_minimal/", name="destination_get_minimal")
   */
  public function getMinimalAction()
  {
    $model = new DestinationModel();
    $data = $model->getAllMinimal();
    $response = new Response(json_encode($data));
    $response->headers->set('Content-Type', 'application/json');
    return $response;
  }

  /**
   * @Route("/guideDestination/getLastAddDestination/", name="destination_last_add_destination")
   */
  public function getLastAddDestinationAction()
  {
    $guide_destination_model = new GuideDestinationModel();
    $data = $guide_destination_model->getLastAddDestination();
    $response = new Response(json_encode($data));
    $response->headers->set('Content-Type', 'application/json');
    return $response;
  }
}

我收到了这个错误:

Attempted to load class "GuideDestinationModel" from namespace "FacilitaTripBundle\Api\models\GuideDestination".
Did you forget a "use" statement for another namespace

我在此文件中定义了GuideDestinationModel类:

namespace FacilitaTripBundle\Api\models\GuideDestination;

class GuideDestinationModel {
}

你怎么知道我为什么会遇到这个错误?

1 个答案:

答案 0 :(得分:1)

包含GuideDestinationModel类的文件的物理路径是什么?

根据Symfony使用的PSR-0,您应该将类​​定义放在与命名空间相对应的路径中,如下例所示。

  

\ Symfony \ Core \ Request =>   /path/to/project/lib/vendor/Symfony/Core/Request.php

我注意到models命名空间中有小写FacilitaTripBundle\Api\models\GuideDestination。您的目录models也是小写的吗?如果不是这可能是这里的问题,如果你使用的系统在路径解析时是区分大小写的(即Linux)