JacobSeated Posted June 23, 2020 Share Posted June 23, 2020 (edited) Hallo, I have a strange problem with a freshly installed composer project that is not loading the dependencies as the documentation states it should. I just made a new directory (www/site/test/), which has a index.php file, and I want to be able to use composer-installed dependencies from within the index.php file, inside the test folder; is this the wrong way to do it? Do I need to place my project root somewhere else for it to work? Dependencies are installed like this: composer require phpmailer/phpmailer For some strange reason, the autoloader is not loading the dependencies, and I simply get a class not found, without any explanation as to why it was not found. The thing is, I can get it to work by resorting to crazy hackery like: new PHPMailer\PHPMailer\PHPMailer() But the documentation clearly states that I should be able to do new PHPMailer() - hope this makes sense. This is what I have in my index.php: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); // define('BASE_PATH', rtrim(preg_replace('#[/\\\\]{1}#', '/', realpath(dirname(__FILE__))), '/') . '/'); // require BASE_PATH . 'vendor/autoload.php'; require __DIR__ . '/vendor/autoload.php'; Edited June 23, 2020 by JacobSeated Quote Link to comment https://forums.phpfreaks.com/topic/310981-composer-not-autoloading-classes-from-vendor-dir-in-indexphp/ Share on other sites More sharing options...
requinix Posted June 23, 2020 Share Posted June 23, 2020 You're reading from some old documentation. PHPMailer\PHPMailer\PHPMailer looks weird but it is correct: the first "PHPMailer" is the organization, the second is the project, and the third is the class itself. https://github.com/PHPMailer/PHPMailer/blob/master/UPGRADING.md 1 Quote Link to comment https://forums.phpfreaks.com/topic/310981-composer-not-autoloading-classes-from-vendor-dir-in-indexphp/#findComment-1579084 Share on other sites More sharing options...
JacobSeated Posted June 23, 2020 Author Share Posted June 23, 2020 5 minutes ago, requinix said: You're reading from some old documentation. PHPMailer\PHPMailer\PHPMailer looks weird but it is correct: the first "PHPMailer" is the organization, the second is the project, and the third is the class itself. https://github.com/PHPMailer/PHPMailer/blob/master/UPGRADING.md Thanks, this is much appreciated. It actually looks like you are right, and this is the default behavior. Maybe this is better documented here, at least for PHPMailer: https://packagist.org/packages/phpmailer/phpmailer Use statements also seem to work. It looks like the vendor/project part is what is listed in composer.json, in the "require" section. I wonder if there is a "standard" way to include this in my index.php file, so I do not have to write all those use statements? I read somewhere else that the vendor/composer/autoload_namespaces.php is supposed to update an array automatically, but the array in my file is just empty. Not sure if that has something to do with it? In any case, this is not much different than how I do things in vanilla PHP, so it does not bother me too much; except, I have to sometimes guess at the right framework/path, since it does not seem to be documented in some cases. I can see that some other packages still mention writing new someName(), without clarifying the part about namespaces (vendor/project). Quote Link to comment https://forums.phpfreaks.com/topic/310981-composer-not-autoloading-classes-from-vendor-dir-in-indexphp/#findComment-1579088 Share on other sites More sharing options...
requinix Posted June 23, 2020 Share Posted June 23, 2020 8 minutes ago, JacobSeated said: Use statements also seem to work. It looks like the vendor/project part is what is listed in composer.json, in the "require" section. I wonder if there is a "standard" way to include this in my index.php file, so I do not have to write all those use statements? It's easier when you contain all your mailing work in one single location instead of spreading calls to it throughout your application. It's perfectly acceptable to create your own mailing class that uses PHPMailer to do the work. Quote I read somewhere else that the vendor/composer/autoload_namespaces.php is supposed to update an array automatically, but the array in my file is just empty. Not sure if that has something to do with it? Don't worry about those files. Quote I can see that some other packages still mention writing new someName(), without clarifying the part about namespaces (vendor/project). It should be mentioned somewhere, but otherwise it's mostly implied. 1 Quote Link to comment https://forums.phpfreaks.com/topic/310981-composer-not-autoloading-classes-from-vendor-dir-in-indexphp/#findComment-1579089 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.