I'm struggling with Codeigniter + PHPMailer via Composer.
I'm getting this error:
Class 'PHPMailer' not found
I have CI version 3.1.5 and I've composed PHPMailer 6.0.0 using the following command:
composer require phpmailer/phpmailer
Inside my root folder, so it created something like this:
/CI_root
|-- application
|-- system
|-- vendor
| |-- phpmailer
| |-- phpmailer
| |-- language
| |-- src
|-- composer.json
Inside my application/config/config.php there is this line
$config['composer_autoload'] = FCPATH."vendor/autoload.php";
PS.: I tried changing to $config['composer_autoload'] = "./vendor/autoload.php"; as well
And finally, the root composer.json
{
"description": "The CodeIgniter framework",
"name": "codeigniter/framework",
"type": "project",
"homepage": "https://codeigniter.com",
"license": "MIT",
"support": {
"forum": "http://forum.codeigniter.com/",
"wiki": "https://github.com/bcit-ci/CodeIgniter/wiki",
"irc": "irc://irc.freenode.net/codeigniter",
"source": "https://github.com/bcit-ci/CodeIgniter"
},
"require": {
"php": ">=5.3.7",
"phpmailer/phpmailer": "^6.0",
"mpdf/mpdf": "^6.1"
},
"suggest": {
"paragonie/random_compat": "Provides better randomness in PHP 5.x"
},
"require-dev": {
"mikey179/vfsStream": "1.1.*",
"phpunit/phpunit": "4.* || 5.*"
}
}
And inside the controller
public function index()
{
$mail = new PHPMailer();
// other of stuff
Funny thing is mpdf loads okay. Am I missing something here?
Thanks in advance!
Composer auto-loading should just be TRUE:
$config['composer_autoload'] = TRUE;
This is because CodeIgniter already knows that your composer autoloaded files will be in /application/vendor
at the top of your file where you want to use PHP mailer, probably need something like:
use PHPMailer;
Actually, in the PHPMailer docs, they show:
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
I just add at the top, before class
use PHPMailer\PHPMailer\PHPMailer;
Then inside the controller method,
simply use it as always:
$mail = new PHPMailer;
Related
I've tried now some different approaches and read articles here and elsewhere but i can't figure out what i'm doing wrong.
I'm developing a package and I want it to be installed via symlink in a different project to develop both parts simultaneously without the need of updating the dependencies over and over again.
The thing I cant get to work is the autoloading.
Here is my setup:
Project composer.json
{
"repositories": [
{
"type": "path",
"url": "../../FormTableComponent",
"options": {
"symlink": true
}
}
],
"require": {
[...]
"bluechord/formtablecomponent": "#dev"
}
}
FormtableComponent Folder Structure
FormTableComponent/
src/
| Container.php
composer.json
FormtableComponent composer.json
{
"name": "bluechord/formtablecomponent",
"description": "...",
"autoload": {
"psr-4": {
"BlueChord\\FormTableComponent\\": "src/"
}
},
"require" : {
[...]
}
}
FormtableComponent Container.php
<?php
/**
* FormTable Container. The Container Class that registers all Parts of the
* Component.
*/
namespace BlueChord\FormTableComponent;
class Container {
function __construct() {
}
}
When I try to use the class and instantiate it I get
Uncaught Error: Class 'BlueChord\FormTableComponent\Container' not found
Thanks for your help!
ADDITIONAL INFO:
A simple Test.php which reproduces the error inside the project
<?php
require_once 'bin/vendor/autoload.php';
echo "BCOSP Test";
use DebugBar\StandardDebugBar;
use BlueChord\FormTableComponent\Container;
$debugbar = new StandardDebugBar(); --> WORKS
echo StandardDebugBar::class ."\n";
$container = new Container(); --> ERROR
echo Container::class . "\n";
Composer
The vendor/composer/autoload_psr4.php does NOT contain any array key for my package.
If I run composer dump-autoload inside my main project nothing changes.
If I run composer dump-autoload inside my package it creates the correct autoload_psr4.php
I have installed hybridauth via composer and then followed the steps given here, but always get the following error:
Could not load the Hybrid_Auth class
Any solution to this?
Use:
$this->load->library('hybridauth');
in your controller to load the library.
application/composer.json
{
"require" : {
"hybridauth/hybridauth" : "~3.0"
},
"config": {
"platform": {
"php": "X.X.XX" //SET your php version
}
}
}
and change config file application/config/config.php
$config['composer_autoload'] = TRUE;
I'm creating a web app with Slim and Twig. The libraries I use work perfectly, I can call them easily with no problem. However my own classes are not found by composer.json autoload psr-4 (psr-0 doesn't find them either)
Here is my file system:
project
|composer.json
|src
|public
| |index.php
|classes
| |Application.php
| |middlewares
| |SecurityMiddleware.php
|templates
|TemplateController.php
|main
|MainController.php
Here is my composer.json:
{
"authors": [
{
"name": "Jean-Marc ZIMMER",
"email": "#################gmail.com",
"role": "Developer"
}
],
"require": {
"slim/slim": "^3.11",
"slim/extras": "*",
"twig/twig": "^2.5",
"slim/twig-view": "^2.4",
"slim/views": "^0.1.3"
},
"autoload": {
"psr-4": {
"src\\": "src",
"middlewares\\": "src/classes/middlewares",
"classes\\": "src/classes",
"templates\\": "src/templates"
}
}
}
Then src/classes/Application.php:
<?php
namespace classes;
class Application extends \Slim\App {
public function __construct($container = array()) {
parent::__construct($container);
}
}
And finally my index.php file:
<?php
require '../../vendor/autoload.php';
$app = new \classes\Application([
"settings" => [
"displayErrorDetails" => true
]
]);
$app->run();
When I run composer dump-autoload, the command outputs:
Generated autoload files containing 0 classes
then exits with status code 0. It should find 4 classes, right ?
And running the app shows the error:
Fatal error: Uncaught Error: Class 'classes\Application' not found in /opt/lampp/htdocs/project/src/public/index.php:5
I'm sure I'm missing something, indicating a namespace or something. Can anyone help me ?
Edits:
I tried using the --optimize or the --classmap-authoritative option for dump-autoload. Changed nothing.
Adding a '/' to the folder names in composer.json doesn't change anything.
I got a solution from another source. I don't personally like it, but it works.
The file system wasn't changed.
composer.json autoload:
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
src/public/index.php:
<?php
require '../../vendor/autoload.php';
$app = new \App\classes\Application([
"settings" => [
"displayErrorDetails" => true
]
]);
$app->run();
src/classes/Application.php:
<?php
namespace App\classes;
class Application extends \Slim\App {
public function __construct($container = array()) {
parent::__construct($container);
}
}
I'm going to work from this functional base and see if I can get the result I want. If I do, I'll edit this answer.
Ensure your composer.json references your deployment paths. For example:
Dockerfile
FROM php:7.2-apache
COPY src /var/www/html
COPY vendor /var/www/vendor
composer.json
{
"autoload": {
"psr-4": {
"Acme\\": "html/classes/"
}
}
}
i.e. html/classes/ not src/classes/
I recently started with TYPO3 and composer as a student. I'm kind of new to all this and I can't get my composer to autoload an api library from Mautic that I want to have. My code keeps telling me that it can't find the classes.
In my extension root directory I have composer.json and it looks like this
{
"name": "woeler\/wlr_typo_mautic",
"config": {
"vendor-dir": "Libraries"
},
"description": "Typo3 to Mautic connection",
"type": "typo3-cms-extension",
"require": {
"mautic\/api-library": "^2.6"
},
"autoload": {
"psr-4": {
"Woeler\\WlrTypoMautic\\": "Classes",
"Mautic\\Auth\\": "Libraries\/mautic\/api-library\/lib\/Auth\/"
}
},
"license": "MIT",
"authors": [
{
"name": "MyName",
"email": "myemail#domain.tld"
}
]
}
I'm probably making a basic mistake, but the tutorials I can find are not really helping me. I have a class in the folder Classes/Controller and I want it to use a class that should fall under the namespace Mautic\Auth\ but it simply tells me it cannot be found.
The actual location of the mautic class is [extension root]/Librariesmautic/api-library/lib/Auth/ApiAuth.php
I require my Libraries/autoload.php in my ext_tables.php file.
Can anyone see what I'm doing wrong?
the package mautic/api-library brings his own composer.json with the psr-4 autoload section, so you don't need to add it to your composer.json.
If you require a package it will installed in the vendor folder (whatever the root composer.json is targeting it), so you can't be able to know where the package will be located.
Just remove the psr-4 section and let the composer autoloader do its magic.
When running the example code on the laravel docs php artisan make:request StoreBlogPostRequest to create a new validation controller, I get the following error
[RuntimeException]
Unable to detect application namespace.
I'm not sure what's wrong, I've done some searching, but nothing really explains this error. Any ideas?
In Laravel 5, an "application" is a collection of PHP files under a single namespace, stored in the folder app/
By default, and in most of the Laravel 5 sample code from the docs, this namespace is App\. For example, one controller in your application might look like this.
namespace App\Http\Controller;
class MyController
{
//...
}
When Laravel generates code (i.e. when you use the make:request command), it needs to know what this application namespace is (it's possible to change the namespace with the artisan app:name command). For some reason, in your system, Laravel 5 can't detect the namespace.
If you look at the section of Laravel 5 core code that detects the namespace
#File: vendor/laravel/framework/src/Illuminate/Console/AppNamespaceDetectorTrait.php
protected function getAppNamespace()
{
$composer = json_decode(file_get_contents(base_path().'/composer.json'), true);
foreach ((array) data_get($composer, 'autoload.psr-4') as $namespace => $path)
{
foreach ((array) $path as $pathChoice)
{
if (realpath(app_path()) == realpath(base_path().'/'.$pathChoice)) return $namespace;
}
}
throw new RuntimeException("Unable to detect application namespace.");
}
You'll see that Laravel detects the namespace by looking at your composer.json file, and looking for thefirst valid psr-4 namespace.
My guess is your composer.json file is missing the namespace
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
Add that back in, and you'll be good to go.
Usually, this error can be mapped to syntax issues or errors in composer.json file. Check for any trailing commas or Auto load issue. For e.g.
"require-dev": {
"barryvdh/laravel-debugbar": "^3.5",
"phpunit/phpunit": "^7.5",
},
This should be..
"require-dev": {
"barryvdh/laravel-debugbar": "^3.5",
"phpunit/phpunit": "^7.5"
},
See no trailing commas at the end of "phpunit/phpunit": "^7.5"