【问题标题】:The autoloader expected class [...] to be defined in file自动加载器预期的类 [...] 在文件中定义
【发布时间】:2016-11-02 15:32:29
【问题描述】:

我是 Symfony 3 的新手。我尝试解决我的两个控制器的问题。 当我执行 indexAction 函数时,我遇到了这个错误:

自动加载器预期类“Arcturus\GeomancieBundle\Controller\TirageController”将在文件“/Applications/MAMP/htdocs/geomancie2/geomancie/vendor/composer/../../src/Arcturus/GeomancieBundle/Controller /TirageController.php”。找到文件但类不在其中,类名或命名空间可能有错别字。

我发现这可能是课堂上的拼写错误……但没有发现任何错误。

这是我的两个控制器:

DefaultController.php

<?php

namespace Arcturus\GeomancieBundle\Controller;
namespace Arcturus\GeomancieBundle\Entity;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form;
use Symfony\Component\HttpFoundation\Request;
use Arcturus\GeomancieBundle\Entity;

class DefaultController extends Controller
{
    public function indexAction(Request $request) {
        $tirage = new Tirage();
        $formTirage = $this->createFormBuilder($tirage)->getForm();

        // Si le formulaire a été soumis

        $formTirage->handleRequest($request);

        if ($formTirage->isSubmitted() && $formTirage->isValid()) {
            $tirage = $formTirage->all();

            return $this->redirectToRoute('arcturus_geomancie_tirage', $tirage);
        }

        // Si le formulaire n'a pas été soumis

        return $this->render('ArcturusGeomancieBundle:Default:index.html.twig', array(
           'form' => $formTirage->createView(),
        ));
    }
}

TirageController.php

<?php

namespace Arcturus\GeomancieBundle\Controller;
namespace Arcturus\GeomancieBundle\Entity;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class TirageController extends Controller
{
    public function afficherTirageAction(Tirage $tirage)
    {
        // On compte le nombre de points dans chaque chaîne
        $nb_dot_l1 = $this->nbDot($tirage->getLigne1());
        $nb_dot_l2 = $this->nbDot($tirage->getLigne2());
        $nb_dot_l3 = $this->nbDot($tirage->getLigne3());
        $nb_dot_l4 = $this->nbDot($tirage->getLigne4());

        // On vérifie que les 4 chaînes contriennent au moins 1 point
        if ($nb_dot_l1 == 0 or $nb_dot_l2 == 0 or $nb_dot_l3 == 0 or $nb_dot_l4 == 0) {
            // On renvoie sur une page d'erreur
            $this->renderView('@ArcturusGeomancie/Default/erreur_tirage.html.twig');
        }

        // On charge les lignes dans un tableau paire/impaire
        $tab_dots_lines = $this->dots_to_array($nb_dot_l1, $nb_dot_l2, $nb_dot_l3, $nb_dot_l4);
        // On garde ce format pour dessiner la figure
        $data['dessin'] = $tab_dots_lines;
        // On récupère le nom de la figure
        $data['figure'] = $this->get_figure($tab_dots_lines);
        // On récupère l'analyse associée
        $data['analyse'] = $this->get_analysis($data['figure']);

        $this->renderView('@ArcturusGeomancie/Default/tirage.html.twig', $data);
    }
[...]

Tirage.php(实体)

<?php

class Tirage
{
    private $ligne1;
    private $ligne2;
    private $ligne3;
    private $ligne4;

    public function getLigne1()
    {
        return $this->ligne1;
    }

    public function getLigne2()
    {
        return $this->ligne2;
    }

    public function getLigne3()
    {
        return $this->ligne3;
    }

    public function getLigne4()
    {
        return $this->ligne4;
    }
}

?>

还有我的目录树:

谁能帮我找出我的错误?

谢谢你:)

【问题讨论】:

    标签: symfony


    【解决方案1】:

    您的文件中的命名空间存在问题。

    删除线

    namespace Arcturus\GeomancieBundle\Entity;
    

    从DefaultController.php和TirageController.php,放到Tirage.php中

    <?php
    
    namespace Arcturus\GeomancieBundle\Entity;
    
    class Tirage
    {
    

    您可以在此处阅读有关命名空间的更多信息:http://php.net/manual/en/language.namespaces.php

    【讨论】:

    • 谢谢,现在我得到了:Arcturus\GeomancieBundle\Controller\Tirage 类不存在。有什么想法吗?
    • CRITICAL - 未捕获的 PHP 异常 ReflectionException:“类 Arcturus\GeomancieBundle\Controller\Tirage 不存在”在 /Applications/MAMP/htdocs/geomancie2/geomancie/vendor/sensio/framework-extra-bundle/ EventListener/ParamConverterListener.php 第 83 行
    • 您的方法 afficherTirageAction() 采用一个参数 - 类 Arcturus\GeomancieBundle\Entity\Tirage
      的实例,但是您缺少该类的使用语句,您应该输入语句 'use Arcturus\GeomancieBundle \实体\Tirage;'在 TirageController.php ...更多信息:php.net/manual/en/language.namespaces.importing.php
    【解决方案2】:

    对于那些使用

    这是一个巧妙的技巧。将 src 目录标记为“Sources Root”,这样每当在您的 src 目录中创建文件时,它都会自动添加正确的命名空间。

    这将有助于避免错误:

    自动加载器预期的类 […] 将在文件中定义

    这是一张图片,展示了如何将您的目录标记为“Sources Root”。

    【讨论】:

    • 您单击了哪个菜单选项以获取您在此处显示的下拉菜单?或许更重要的是,您是如何让 phpStorm 了解您在服务器上的目录结构的?
    • @HowardBrown 右键单击​​src 目录。 “服务器上的目录结构”是什么意思?
    【解决方案3】:

    你的命名空间有点过时了,你只是在 put 类中放了一个。

    控制器应该有一个单一的命名空间;

    namespace Arcturus\GeomancieBundle\Controller;
    

    并且您的实体应该具有以下命名空间;

    namespace Arcturus\GeomancieBundle\Entity;
    

    另外,您是否正确设置了路由? 如果您正在使用注释,例如(在 app/config/routing.php 中);

    app:
        resource: "@GeomancieBundle/Controller/"
        type: annotation
    

    my_route:
        path: /my-url
        defaults:   {_controller: ArcturusGeomancieBundle:Tirage:afficherTirage }
    

    【讨论】:

      【解决方案4】:

      我发现同样的错误是针对不同的问题。 我将一个文件夹从 Appbundle/controller/Myfolder 移动到 Appbundle/Myfolder。 之后,我不断收到该错误。 我使用 git 并且服务器版本与本地版本不匹配,所以我看不到服务器中有重复的文件夹,因为自动上传没有删除

      因此,如果在删除文件时上传失败,手动更新/同步您的服务器可能有助于解决此错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-03
        • 2013-06-02
        • 1970-01-01
        相关资源
        最近更新 更多