Symfony - 尝试加载类 - ClassNotFoundException

时间:2016-12-16 08:49:18

标签: php symfony

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

  

尝试加载类" ApiProblemException"来自命名空间" AppBundle \ Libraries \ Core \ Api"。   你忘记了"使用"另一个名称空间的语句   500内部服务器错误 - ClassNotFoundException

但我有使用声明use AppBundle\Libraries\Core\Api\ApiProblemException;

文件位置:

  • ApiCityselectionController: Project \ src \ AppBundle \ Controller \ Api \ User \ ApiCityselectionController.php

  • ApiProblem: Project \ src \ AppBundle \ Libraries \ 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);
    }
}

ApiCityselectionController:

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 个答案:

答案 0 :(得分:1)

您的文件名中有拼写错误

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

应该是

ApiProblemEx <强> C eption.php