2016-12-16 55 views
0

我想在Symfony控制器中使用类ApiProblemException。 这应该返回一个API的错误响应。 当我在AJAX请求体发送错误的参数,使throw new ApiProblemException($apiProblem);执行我得到:Symfony - 试图加载类 - ClassNotFoundException

尝试从命名空间“的appbundle \库\核心\ API”装入类“ApiProblemException”。 你忘记了另一个命名空间的“使用”语句吗? 500内部服务器错误 - ClassNotFoundException的

但我使用的语句use AppBundle\Libraries\Core\Api\ApiProblemException;

文件地点:

  • ApiCityselectionController:项目的\ src \的appbundle \控制器\阿比\用户\ ApiCityselectionController.php

  • ApiProblem:项目的\ src \的appbundle \库\ Core \ Api \ ApiProblem.php

  • ApiProblemExeption: 项目的\ src \的appbundle \库\核心\阿比\ ApiProblemExeption.php

我的代码:

ApiProblem:

namespace AppBundle\Libraries\Core\Api; 

use Symfony\Component\HttpFoundation\Response; 

/** 
* A wrapper for holding data to be used for a application/problem+json response 
*/ 
class ApiProblem 
{ 
    //Validation 
    const TYPE_VALIDATION_ERROR = 'validation_error'; 

    //Wrong input 
    const TYPE_INVALID_REQUEST_BODY_FORMAT = 'invalid_body_format'; 
    const TYPE_INVALID_REQUEST_BODY_DATATYPE = "invalid_body_datatype"; 

    //Not found 
    const TYPE_USER_NOT_FOUND = "user_not_found"; 
    const TYPE_CITY_NOT_FOUND = "city_not_found"; 

    private static $titles = array(
     self::TYPE_VALIDATION_ERROR => 'Ein Validierungsfehler ist aufgetreten', 
     self::TYPE_INVALID_REQUEST_BODY_FORMAT => 'Ungültiges JSON gesendet', 
     self::TYPE_INVALID_REQUEST_BODY_DATATYPE => "Falschen Datentyp gesendet", 
     self::TYPE_USER_NOT_FOUND => "Benutzer konnte nicht gefunden werden", 
     self::TYPE_CITY_NOT_FOUND => "Gemeinde konnte nicht gefunden werden", 
    ); 
    private $statusCode; 
    private $type; 
    private $title; 
    private $extraData = array(); 

    public function __construct($statusCode, $type = null) 
    { 
     $this->statusCode = $statusCode; 
     if ($type === null) 
     { 
      // no type? The default is about:blank and the title should 
      // be the standard status code message 
      $type = 'about:blank'; 
      $title = isset(Response::$statusTexts[$statusCode]) 
       ? Response::$statusTexts[$statusCode] 
       : 'Unknown status code :('; 
     } 
     else 
     { 
      if (!isset(self::$titles[$type])) 
      { 
       throw new \InvalidArgumentException('No title for type '.$type); 
      } 
      $title = self::$titles[$type]; 
     } 
     $this->type = $type; 
     $this->title = $title; 
    } 

    public function toArray() 
    { 
     return array_merge(
      $this->extraData, 
      array(
       'status' => $this->statusCode, 
       'type' => $this->type, 
       'title' => $this->title, 
      ) 
     ); 
    } 

    public function set($name, $value) 
    { 
     $this->extraData[$name] = $value; 
    } 

    public function getStatusCode() 
    { 
     return $this->statusCode; 
    } 

    public function getTitle() 
    { 
     return $this->title; 
    } 
} 

ApiProblemExeption:

namespace AppBundle\Libraries\Core\Api; 

use Symfony\Component\HttpKernel\Exception\HttpException; 

class ApiProblemException extends HttpException 
{ 
    private $apiProblem; 

    public function __construct(ApiProblem $apiProblem, \Exception $previous = null, array $headers = array(), $code = 0) 
    { 
     $this->apiProblem = $apiProblem; 
     $statusCode = $apiProblem->getStatusCode(); 
     $message = $apiProblem->getTitle(); 
     parent::__construct($statusCode, $message, $previous, $headers, $code); 
    } 
} 

ApiCityse lectionController:

namespace AppBundle\Controller\Api\User; 

use AppBundle\Libraries\Core\Api\ApiProblem; 
use AppBundle\Libraries\Core\Api\ApiProblemException; 
use AppBundle\Libraries\Data\Users; 
use AppBundle\Libraries\Core\EntitySerializer; 
use AppBundle\Libraries\Core\Validator; 
use FOS\RestBundle\Controller\FOSRestController; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Symfony\Component\HttpFoundation\JsonResponse; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpKernel\Exception\HttpException; 

class ApiCityselectionController extends FOSRestController 
{ 
    /** 
    * @Route("/", name="api_user_cityselection") 
    * @Method("POST") 
    * @param Request $request 
    * @return JsonResponse 
    * Sets the city of a user 
    */ 
    public function CityselectionAction(Request $request) 
    { 
     $validator = new Validator(); 

     $userId = json_decode($request->request->get('userId')); 
     $cityId = json_decode($request->request->get('cityId')); 

     $isUserIdValid = $validator->validateInt($userId); 
     $isCityIdValid = $validator->validateInt($cityId); 

     if($isUserIdValid && $isCityIdValid) 
     { 
      $user = $this->getDoctrine() 
       ->getRepository('AppBundle:Users') 
       ->findOneBy(array('id' => $userId)); 

      $city = $this->getDoctrine() 
       ->getRepository('AppBundle:Cities') 
       ->findOneBy(array('id' => $cityId)); 

      $isUserValid = $validator->validateEntity($user); 

      //Check if user exists 
      if (!$isUserValid) 
      { 
       $apiProblem = new ApiProblem(400, ApiProblem::TYPE_USER_NOT_FOUND); 

       throw new ApiProblemException($apiProblem); 
      } 

      $isCityValid = $validator->validateEntity($city); 

      if($isUserValid && $isCityValid) 
      { 
       $user->setFkCity($city); 

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

       $serializer = new EntitySerializer($this->getDoctrine()->getManager()); 
       $responseData = $serializer->serializeUser($user); 
      } 
      else 
      { 
       $apiProblem = new ApiProblem(400, ApiProblem::TYPE_CITY_NOT_FOUND); 

       throw new ApiProblemException($apiProblem); 
      } 
     } 
     else 
     { 
      $apiProblem = new ApiProblem(400, ApiProblem::TYPE_INVALID_REQUEST_BODY_DATATYPE); 

      throw new ApiProblemException($apiProblem); 
     } 

     $response = new JsonResponse($responseData, 200); 
     return $response; 
    } 
} 

不执行代码throw new ApiProblemException($apiProblem);时,它的工作原理。

回答

1

你有一个错字在你的文件名

项目的\ src \的appbundle \库\核心\阿比\ ApiProblemExeption.php

应该

ApiProblemEx Ç eption.php

+0

谢谢,man这个错误很烦人.. – Orlando

相关问题