Symfony 2 classnotfoundexception

时间:2014-03-17 12:11:04

标签: php class symfony

我遇到了symfony 2的问题。一切正常,直到某一点。

我有一个简单的控制器动作,一个实体类和一个表单(抽象类型)类。我收到了这个问题:

  

ClassNotFoundException:尝试从/var/www/dellf2/src/Blogger/BlogBu​​ndle/Controller/PageController.php第26行中的命名空间“Symfony \ Blogger \ BlogBu​​ndle \ Entity”加载类“Inquiry”。你需要“使用“来自另一个名称空间?”

我查看了Enquiry.php,它位于正确的位置(Blogger / BlogBu​​ndle / Entity / Enquiry.php)。我检查了权限,它看起来很好(755)。即使我尝试使用include_once()包含该文件,它也无法正常工作。

Enquiry.php     

// src/Blogger/BlogBundle/Entity/Enquiry.php

namespace Blogger\BlogBundle\Entity;

class Enquiry
{

protected $name;
protected $email;
protected $subject;
protected $body;

public function getName()
{
    return $this->name;
}

public function setName($name)
{
    $this->name = $name;
}

public function getEmail()
{
    return $this->email;
}

public function setEmail($email)
{
    $this->email = $email;
}

public function getSubject()
{
    return $this->subject;
}

public function setSubject($subject)
{
    $this->subject = $subject;
}

public function getBody()
{
    return $this->body;
}

public function setBody($body)
{
    $this->body = $body;
}

}

?>

PageController.php

<?php

// src/Blogger/BlogBundle/Controller/PageController.php

 namespace Blogger\BlogBundle\Controller;
 //include_once('../Entity/Enquiry.php');

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Blogger\BlogBundle\Entity\Enquiry;
use Symfony\Blogger\BlogBundle\Form\EnquiryType;

  class PageController extends Controller
{
public function indexAction()
{
    return $this->render('BloggerBlogBundle:Page:index.html.twig');
}

public function aboutAction()
{
    return $this->render('BloggerBlogBundle:Page:about.html.twig');
}

public function contactAction()
{
    $enquiry = new Enquiry();
    $form = $this->createForm(new EnquiryType(), $enquiry);

    $request = $this->getRequest();
    if($request->getMethod() == 'POST'){
        $form->bindRequest($request);

        if($form->isValid()){
            //Perform some action, such as sending an email

            //Redirect - This is important to prevent users re-posting the form if they refresh the page
            return $this->redirect($this->generateUrl('BloggerBlogBundle_contact'));
        }
    }
    return $this->render('BloggerBlogBundle:Page:contact.html.twig', array('form'=> $form->createView()));
}
 }

2 个答案:

答案 0 :(得分:3)

在同一个例子中,出现了类似的错误。

Attempted to load class "MaxLength" from namespace "Symfony\Component\Validator\Constraints".
Did you forget a "use" statement for another namespace?
500 Internal Server Error - ClassNotFoundException

       $metadata->addPropertyConstraint('subject', new NotBlank());
 -->   $metadata->addPropertyConstraint('subject', new MaxLength(50));
       $metadata->addPropertyConstraint('body', new MinLength(50));

结束了问题是使用折旧的类集。

而不是使用:

use Symfony\Component\Validator\Constraints\MinLength;
use Symfony\Component\Validator\Constraints\MaxLength;

$metadata->addPropertyConstraint('subject', new MaxLength(50));
$metadata->addPropertyConstraint('body', new MinLength(50));

您应该使用:

use Symfony\Component\Validator\Constraints\Length;

$metadata->addPropertyConstraint('subject', new Length(array('max' => 50)));
$metadata->addPropertyConstraint('body', new Length(array('min' => 50)));

参考文献:

http://symfony.com/doc/2.1/reference/constraints/Length.html http://symfony.com/doc/2.1/reference/constraints/MinLength.html

  

自版本2.1起,MinLength约束已弃用,并且将是   在Symfony 2.3中删除。请使用长度和最小选项。

答案 1 :(得分:1)

尝试添加

use \Blogger\BlogBundle\Entity\Enquiry;

在命名空间行下。

也许自动加载器在错误的地方进行搜索。