【问题标题】:Symfony 2 classnotfoundexceptionSymfony 2 类未发现异常
【发布时间】:2014-04-22 15:12:12
【问题描述】:

我在使用 symfony 2 时遇到了问题。在某个点之前一切正常。

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

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

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

查询.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()));
}
 }

【问题讨论】:

    标签: php class symfony


    【解决方案1】:

    在同一个示例中,弹出了类似的错误。

    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

    MinLength 约束自 2.1 版起已弃用,并将 在 Symfony 2.3 中删除。请改用 Length 和 min 选项。

    【讨论】:

      【解决方案2】:

      尝试添加

      use \Blogger\BlogBundle\Entity\Enquiry;
      

      在命名空间行下。

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

      【讨论】:

      • 感谢您的及时回复。但是我需要在 PageController.php 中添加它吗?我已经在那里了。
      • 您的实体似乎不在 Symfony 命名空间中,因此请尝试从两个 Blogger 使用子句中删除“Symfony”。
      • 谢谢 Michal,没错,这是我的问题。
      猜你喜欢
      • 2010-12-11
      • 2016-03-06
      • 2012-09-19
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多