6
votes

I'm new to Symfony 3. I try to resolve a problem with my two controllers. When I execute the indexAction function I've got this error :

The autoloader expected class "Arcturus\GeomancieBundle\Controller\TirageController" to be defined in file "/Applications/MAMP/htdocs/geomancie2/geomancie/vendor/composer/../../src/Arcturus/GeomancieBundle/Controller/TirageController.php". The file was found but the class was not in it, the class name or namespace probably has a typo.

I've found that can be a typo in a class...but did'nt find anything wrong.

Here's my two controllers :

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 (Entity)

<?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;
    }
}

?>

And my directory tree :

enter image description here

Could anyone help me to find my error ?

Thank you :)

4

4 Answers

14
votes

There is a problem with namespaces in your files.

Remove line

namespace Arcturus\GeomancieBundle\Entity;

from DefaultController.php and TirageController.php, and put it in Tirage.php

<?php

namespace Arcturus\GeomancieBundle\Entity;

class Tirage
{

You can read more about namespaces here: http://php.net/manual/en/language.namespaces.php

4
votes

For those using

Here is a neat trick. Mark the src directory as "Sources Root", this way whenever a file is created within your src directory, it'll automatically add the correct namespace.

This will help avoid the error:

The autoloader expected class […] to be defined in file

Here is an image showing how to mark your directory as "Sources Root".

mark directory as sources root

2
votes

Your namespaces are a bit out, you just put one in put class.

The controller should have the single namespace of;

namespace Arcturus\GeomancieBundle\Controller;

and your entity should have the namespace of;

namespace Arcturus\GeomancieBundle\Entity;

Also, have you set your routing up properly? If you are using annotations, something like (in app/config/routing.php);

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

or

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

I found this same error for a different problem. I moved one folder from Appbundle/controller/Myfolder to Appbundle/Myfolder. After that i constantly got that error. Im using git and server version wasnt matching the local version so I couldn't see there was a duplicated folder in the server because the auto-upload didnt delete

So updating/syncronizing manually your server might help with this error if the upload fails on deleting files.