Submitted form, returns Class not found autoload Composer

Asked

Viewed 78 times

2

I tried to see if anyone had been through this problem but so far I haven’t found anything on the Internet. I’ve been trying to figure it out for two days and I’m almost giving up.

Directory

index.php
src/
   |__Route/
   |       |__login.php
   |__Dao/
         |__UserDao.php
vendor/

Composer.json

"autoload": {
     "psr-4": {
         "Src\\": "src/"
     }
}

Userdao.php

<?php
namespace Src\Dao;

class UserDao {}

Index.php

<?php 
require_once 'vendor/autoload.php';

login.php

<?php
use Src\Dao\UserDao;

In my index.php works normally, everything is loaded. The problem arises when the form is submitted, arrives at login.php and launches the Fatal error: Uncaught Error: Class FILE not found.

Thank you in advance, in case anyone knows where I’m going wrong.

  • You added autoload require in arqivo login.php as well?

  • If you add it, it will work. But I thought it was just an autoload require. Cannot do with a single require in the file that will start the system?

  • This would be possible if all requests passed through index.php. As your form redirects directly to the login.php file, you need to include autoload in it as well

  • It would be the concept of Frontcontroller

  • Got it, thanks for the feedback Everton.

1 answer

0


Your problem is related to the loading of 'vendor/autoload.php';, it is necessary to load it, as you are working with distinct php files it is necessary, and in your case just add it in login.php:

<?php
require_once 'vendor/autoload.php';
use Src\Dao\UserDao;

Composer autoloading

Browser other questions tagged

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