【问题标题】:Symfony , Autoloader class was not in itSymfony,Autoloader 类不在其中
【发布时间】:2017-02-01 22:09:34
【问题描述】:

我遇到了这个错误,我也因此而发疯。 我正在使用 Symfony 3.2.2 框架,Autoloader 声称找到了文件但类不在其中,类名或命名空间可能有错字。

堆栈跟踪

throw new \RuntimeException(sprintf('Trying to autoload a class with an invalid name "%s". Be careful that the namespace separator is "\" in PHP, not "/".', $class));
            }
            throw new \RuntimeException(sprintf('The autoloader expected class "%s" to be defined in file "%s". The file was found but the class was not in it, the class name or namespace probably has a typo.', $class, $file));
        }
        if (self::$caseCheck) {
            $real = explode('\\', $class.strrchr($file, '.'));

这是我的 BookType.php;

    <?php

namespace Bookkeeeper\ManagerBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class BookType extends AbstractType{

    public function buildForm(FormBuilderInterface $builder, array $options){
        $builder->add('title')->add('description')->add('pages');

    }
    public function setDefaultOptions(OptionsResolverInterface $resolver){
        $resolver->setDefault(array('data_class'=>'Bookkeeeper\ManagerBundle\Entity\Book'));

    }
    public function getName(){
        return 'bookkeeeper_managerbundle_book';
    }



}

**这是我的 BookController.php 文件 **

<?php

namespace Bookkeeper\ManagerBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Bookkeeper\ManagerBundle\Entity\Book;
use Bookkeeper\ManagerBundle\Form\BookType;

class BookController extends Controller
{

    public function indexAction(){
        return $this->render('BookkeeperManagerBundle:Book:index.html.twig');
    }
    public function showAction($id){
        return $this->render('BookkeeperManagerBundle:Book:show.html.twig');    
    }
    public function newAction(){
        $book = new Book();

        $form = $this->createForm(new BookType(), $book, array (
            'action' =>$this->generateUrl('book_create'),
            'method'=> 'POST'
        )); 
        $form->add('submit' , 'submit' , array('label' => 'Create Book'));

        return $this->render('BookkeeperManagerBundle:Book:new.html.twig', array(
            'form'=>$form->createView()
        ));

    }
    public function createAction(Request $request){

    }
    public function editAction($id){
        return $this->render('BookkeeperManagerBundle:Book:edit.html.twig');
    }
    public function updateAction(Request $request, $id){

    }
    public function deleteAction(Request $request, $id){

    }
}

【问题讨论】:

  • 预计什么课程?
  • 自动加载器期望类“Bookkeeper\ManagerBundle\Form\BookType”在文件“/home/huseyinocal/bookkeeper/vendor/composer/../../src/Bookkeeper/ManagerBundle/”中定义表单/BookType.php”。找到文件但类不在其中,类名或命名空间可能有错字。

标签: php symfony


【解决方案1】:

你写了命名空间Bookkeeeper\ManagerBundle\Form;而不是命名空间Bookkeeper\ManagerBundle\Form; (注意你写的额外的 e)

<?php

namespace Bookkeeper\ManagerBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class BookType extends AbstractType{

    public function buildForm(FormBuilderInterface $builder, array $options){
        $builder->add('title')->add('description')->add('pages');

    }
    public function setDefaultOptions(OptionsResolverInterface $resolver){
        $resolver->setDefault(array('data_class'=>'Bookkeeeper\ManagerBundle\Entity\Book'));

    }
    public function getName(){
        return 'bookkeeeper_managerbundle_book';
    }



}

【讨论】:

  • 我的文件名为 BookController.php 而不是 Bookkeeper.Controller.php 。我刚才写错了并编辑了这个。
  • 我用您对@u_mulder 的回复评论编辑了我的答案
  • 非常感谢你。我肯定很复杂。
猜你喜欢
  • 1970-01-01
  • 2012-02-07
  • 2015-07-30
  • 1970-01-01
  • 2018-09-16
  • 1970-01-01
  • 2014-03-27
  • 1970-01-01
  • 2015-12-16
相关资源
最近更新 更多