0

I have a requirement to use PHPOffice/PhpSpreadsheet, to install PhpSpreadsheet I followed this link.

So I installed PhpSpreadsheet into my project using 'composer' by running the command

composer require phpoffice/phpspreadsheet

at command prompt while installing it said that

Using version dev-master for phpoffice/phpspreadsheet

and was installed successfully(FYI check Screenshot), after installing when I tried to login into my application, I am getting a different error as

Cannot redeclare PHPMailerAutoload() 

(previously declared in" for emails I am using 'phpmailer' including the path as

require_once('../vendor/phpmailer/PHPMailerAutoload.php');

as my "PHPMailer" folder resides in 'vendor' folder of Yii2 framework.

By the way I am building my application on Yii2 framework and I am very new to Yii2 framework.

Can anyone please tell, how to fix this? Thanks.

enter image description here

Prasad Patel
  • 707
  • 3
  • 16
  • 53

1 Answers1

2

You don't have to use require for packages installed via composer. Yii autoload's feature will take care for that. You just have to write the appropriate use statements in your code. Actually, the mailer is a component declared into the main app, so you don't need the use statement at all

Yii::$app->mailer->compose('/my/mail_view', ['model' => $model])
                ->setFrom([Yii::$app->params['myMail'] => 'My Name'])
                ->setTo($model->email)
                ->setSubject('My subject')
                ->send();
Chux
  • 1,196
  • 1
  • 9
  • 24
  • I have included 'PHPMailer' folder manually in the 'vendor' of Yii2. So thats why I use 'require'. All this done before installing 'phpSpreadsheet' through composer. – Prasad Patel May 24 '18 at 06:37
  • There's no point to "including manually" modules to `vendor`. It won't be autoloaded until properly added to composer autoload classes! And you should never edit `vendor` manually! – Yupik May 24 '18 at 06:40
  • You don't need the require. The mailer is a component which can be accesed via Yii:$app->mailer (please, see my updated answer). – Chux May 24 '18 at 06:40
  • As a rule of thumb, while your using Yii2, never require. Is you need to use something installed via composer, just write the use statement just before your controller's class declaration. – Chux May 24 '18 at 06:45
  • Sorry, I am a beginner to Yii framework, So, I don't know the usage correctly. I already built an application in corePHP and I am rebuilding that project on Yii2 framework. So, when I built using corePHP I have used 'phpMailer' for emails So, I should use 'phpMailer' only in Yii2 framework also. So, would you tell me how to use it? – Prasad Patel May 24 '18 at 06:46
  • @prasadchinthala you have an example in my answer. Also, please take a look at the Yii's autoload documentation to understand how it works (also linked on my answer). – Chux May 24 '18 at 06:48
  • I got your rule of thumb & understood 'Yii autoload's feature' too. I have fixed it by using 'require' only at both 'login' view & my model locations where I use 'phpmailer' like `require_once('../vendor/phpmailer/PHPMailerAutoload.php')` Thanks. – Prasad Patel May 24 '18 at 07:37
  • Again, see my answer, you have an example illustrating how to use phpmailer with yii. Also, I would advice you to read more about Yii's autoload. – Chux May 24 '18 at 07:39