Reputation: 583
I have ran into a problem with symfony 2. Everything worked fine till a certain point.
I have a simple controller action, a entity class, and a form (abstract type) class. I am getting the issue:
ClassNotFoundException: Attempted to load class "Enquiry" from namespace "Symfony\Blogger\BlogBundle\Entity" in /var/www/dellf2/src/Blogger/BlogBundle/Controller/PageController.php line 26. Do you need to "use" it from another namespace?
I have checked the Enquiry.php and it sits at the right place (Blogger/BlogBundle/Entity/Enquiry.php). I checked permissions and it looks fine(755). Even if I try including the file with include_once(), it isn't working.
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()));
}
}
Upvotes: 0
Views: 3558
Reputation: 19102
In this same example, a similar error was popping up.
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));
Ends up that the problem was using a depreciated set of classes.
Instead of using:
use Symfony\Component\Validator\Constraints\MinLength;
use Symfony\Component\Validator\Constraints\MaxLength;
and
$metadata->addPropertyConstraint('subject', new MaxLength(50));
$metadata->addPropertyConstraint('body', new MinLength(50));
You should use:
use Symfony\Component\Validator\Constraints\Length;
and
$metadata->addPropertyConstraint('subject', new Length(array('max' => 50)));
$metadata->addPropertyConstraint('body', new Length(array('min' => 50)));
References:
http://symfony.com/doc/2.1/reference/constraints/Length.html http://symfony.com/doc/2.1/reference/constraints/MinLength.html
The MinLength constraint is deprecated since version 2.1 and will be removed in Symfony 2.3. Use Length with the min option instead.
Upvotes: 3
Reputation: 697
Try to add
use \Blogger\BlogBundle\Entity\Enquiry;
under the namespace line.
Maybe autoloader is sarching in wrong place.
Upvotes: 1