Error finding class in application using namespace

Asked

Viewed 30 times

-1

I am implementing the namespace with psr-4 in a small PHP application and it turns out that he is not finding the class Mvc\Controller\MyController and I don’t understand the cause

The folder structure is:

src/Controller

src/Model

The class src/Controller/MyController.php that’s how it is

namespace Mvc\Controller;

class MyController
{
    public function listUsers(){
        $model = new \Mvc\Model\MyModel();
        $result = $model->list();
        return $result;
    }
}

The index.php, at the root is:

require_once 'vendor/autoload.php';

print '<h1>Simplest PHP MVC</h1>';
print '<h3>Primeira fase - listar os usuários da tabela.</h3>';

use Mvc\Controller\MyController;
$con = new MyController();

print '<table>';
print '<tr><td><b>ID</td><td><b>Login</td><td><b>Senha</td></tr>';
foreach($con->listUsers() as $user){
    $login = $user->login;
    $id = $user->id;
    $senha = $user->senha;

    print "<tr><td>$id</td><td>$login</td><td>$senha</td>";
    print "<td><a href=\"view/delete.php?id=$id\">Delete</a></td>";
    print "<td><a href=\"view/edit.php?id=$id\">Edit</a></td></tr>";
}
print '</table>';

I’m getting the bug:

Fatal error: Uncaught Error: Class 'Mvc Controller Mycontroller' not found in /var/www/html/mvc/index.php on line 9

If it helps, follow Poser.json

    "autoload":
    {
        "psr-4":
        {
            "Mvc\\" : "src/"
        }
    }
  • I didn’t get to test it, but take that little bar off src/ in the composer.json and comments here if it worked.

  • 1

    Check if the name of the files and folders are correct, may have some typing error

  • 1

    Ever tried to spin one composer dump-autoload ?

  • Lipespry did not change. Constamilan is all right. chavestop already made.

1 answer

0

Run the Composer dump-autoload command

Browser other questions tagged

You are not signed in. Login or sign up in order to post.