Swift_Message not found error after swiftmailer installed in Yii2 - php

I'm setting up a mailer in a project using Yii2. Swiftmailer seemed to be the best solution for this framework so I simply followed the docs.
In composer.json
"yiisoft/yii2-swiftmailer": "*"
Followed by:
composer install
In my config I have: (Yes, I just want it to save a file for now.)
'components' => [
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => true,
],
Then I placed this into a controller:
\Yii::$app->mailer->compose()
->setFrom('from#domain.com')
->setTo('to#domain.com')
->setSubject('Message subject')
->setTextBody('Plain text content')
->setHtmlBody('<b>HTML content</b>')
->send();
My error is:
PHP FATAL - Class 'Swift_Message' not found
I believe, after some research this has to do with the autoloader / lazy loading but the information I can find is spotty. Most of it states that if using composer to install resolves the issue, and all of my troubleshooting has left me chasing my tail.
Here's my stacktrace. Thank you for your help.
Additional:
composer-asset-plugin - IS INSTALLED
vendor/composer/autoload_files.php DOES contain the line:
$vendorDir . '/swiftmailer/swiftmailer/lib/swift_required.php',

I would love just close this embarrassment, but in case someone makes the same assumption as myself, i'd like to help.
Does you entry script contains 'vendor/autoload.php' require? Does it
point to the correct file? Try to manually put die('swiftmailer') at
'vendor/swiftmailer/swiftmailer/lib/swift_required.php' - it should
appear at any application run.
via (klimov-paul / yiisoft)
die('swiftmailer'); had no effect.
vendor/autoload.php is not required.
I had assumed that the other extensions already installed in the vendor folder were in use. I came to find that previous developers were not utilizing anything there and require(__DIR__ . "/../vendor/autoload.php"); was not in the execution path.

Related

Laravel - Nexmo not picking up credentials in .ENV file

I recently added laravel/nexmo-notification-channel to my laravel project which also installed Nexmo/nexmo-laravel.
After installing, I published vendor files so that I get config/nexmo.php and in there I noted that it looks in the .env file for NEXMO_KEY and NEXMO_SECRET.
So I went ahead and created these within my .env file
NEXMO_KEY=[my_key]
NEXMO_SECRET=[my secret]
NEXMO_SIGNATURE_SECRET=[my signature secret]
After this, I added Nexmo to my service providers in app.php:
'providers' => [
...,
Nexmo\Laravel\NexmoServiceProvider::class
]
and also added the following in config/services.php:
'nexmo' => [
'key' => env('NEXMO_KEY', ''),
'secret' => env('NEXMO_SECRET', ''),
'sms_from' => '[my number]'
],
But I still get the following error when thrying to send an SMS using the use Illuminate\Notifications\Messages\NexmoMessage; class:
"message": "Provide either nexmo.api_secret or nexmo.signature_secret",
I can use these same credentials to send an SMS from CLI, so why can't I send it from laravel?
There have been a couple of workarounds for this that are valid, but at first glance it looks like the Nexmo package does the work to bring in the ENV secrets into Laravel's config. Because of caching problems, you should never call env() within Laravel, instead you should be using config() - so in this case, config(nexmo.api_secret).
My main point here though is that I can't look into the "correct" solution for you because the package is abandoned. Nexmo is no longer Nexmo, it's Vonage, and Laravel core team have subsequently updated the notification-channel package.
For supported use to integrate Vonage services (SMS), please use the following package:
https://github.com/laravel/vonage-notification-channel
I'm not sure exactly why, but, Vonage/Nexmo doesn't pick details from the .ENV.
Instead, use a global constant to fetch the secrets:
Create a global.php file in the config folder, and add your secrets from the env like this:
<?php
return [
// Other constants values
'SMS_API_KEY' => env('SMS_API_KEY', ''),
'SMS_API_SECRET' => env('SMS_API_SECRET', ''),
]
?>
Then, you can use the constants in your controller as usual:
'key' => config('global.SMS_API_KEY'),
'secret' => config('global.SMS_API_SECRET')
then: recache, php artisan config:cache

Composer package where to put client config?

I have a composer package designed for use in other projects that has configurations, or rather default settings I'd like the end user to be able to adjust based on their intended use. The package itself can be configured to build things differently depending on the configurations passed to the package's builder class. I'd like there to be default settings, possibly in a settings.yml file (the medium is not a concern, *.conf, *.json, *.php), may be this should go within the package? But then I imagine if that were the case it would be hard for the end user to maintain as it would be overwritten during composer updates? Anyone know what the norm for storing composer vendor packages configs is?
$parameters = [
'handlers' => [
// various depending on client use
'//widget' => 'LivingMarkup\Component\Widgets\{name}',
'//img' => 'LivingMarkup\Component\Img',
'//a' => 'LivingMarkup\Component\A',
'//var' => 'LivingMarkup\Component\Variable',
'//condition' => 'LivingMarkup\Component\Condition',
'//redact' => 'LivingMarkup\Component\Redact'
],
'hooks' => [
'beforeLoad' => 'Executed before onLoad',
'onLoad' => 'Loads object data',
'afterLoad' => 'Executed after onLoad',
'beforeRender' => 'Executed before onLoad',
'onRender' => 'RETURN_CALL',
'afterRender' => 'Executed after onRender',
]
];
Thank you.
After a bit more searching I found a post that addresses this exact question.
https://www.reddit.com/r/PHP/comments/3qqrmz/how_to_handle_config_files_and_default_settings/
To summarize, in case the above link stops working some day, here are some options:
"Bolt CMS does uses a config.yml.dist, which is included with the source code. And the end user can create a config.yml file if one doesn't exist. The config.yml.dist file is part of the official project and acts as the example config file, and should not be modified."
"Your own config.yml file can be tracked in your version control if you wish. Then you can put confidential or environment-specific information into config_local.yml and place that file's name in .gitignore."
"Symfony I use the parameters.yml and config.yml to pass parameters to the objects in DIC."
"Phpunit uses the phpunit.xml.dist file to setup tests."
"Apigen uses .neon or .yaml config files."
"Flysystem asks for config options to be passed directly in the constructor"
"Cakephp has it's own configuration settings, and plugins come with instructions on the keys to add. This asset compress package uses an ini file"
Opus is an option: https://github.com/imarc/opus

webpack-encore-bundle Unknown "encore_entry_script_tags" function

Issue is with symfony 4, php 7.3.7.
I have ran composer require symfony/webpack-encore-bundle and added the default webpack.config.js as recommend in the documentation.
When I add {{ encore_entry_script_tags('app') }} to my base.html.twig I get an error saying that the function is unknown.
Google is leading me nowhere and the documentation says that it is good to go right out of the box if you have symfony flex, which I do.
I tried adding a webpack_encore.yaml to config/packages but it threw an error saying that there is no package to build with this configuration.
Any ideas what I could be missing?
please be sure your config/bundles.php has this line.
return [
...
Symfony\WebpackEncoreBundle\WebpackEncoreBundle::class => ['all' => true],
...
];
also check 'config/packages/webpack_encore.yaml' is exist. Here is default config.
webpack_encore:
output_path: '%kernel.project_dir%/public/build'

Yii2 Class yii\authclient\clients\GoogleOAuth does not exist

I use dektrium/yii2-user (on one project) and yiisoft/yii2-authclient (on another one) to login via Google account.
Some time ago it was everything ok, but i guess after last composer update something was changed and now i get an error: "Class yii\authclient\clients\GoogleOAuth does not exist" when try to open login page.
Does anybody has the same issue or know what's wrong?
Thank you
yii2-authclient has been modified in the latest version which is not backward compatible.
Read about the upgrade process here.
Two solutions:
Modify composer.json to fetch 2.0.6 version (replace * with 2.0.6) - no other changes are needed but no more updates for this extension.
Upgrade your code following the guide in the link above so you can be up-to-date.
In the config file
Replace from
'google' => [
'class' => 'yii\authclient\clients\GoogleOAuth',
..
],
Replace to
'google' => [
'class' => 'yii\authclient\clients\Google',
...
],
Use yii\authclient\clients\Google instead of yii\authclient\clients\GoogleOAuth in your config file.

Laravel 5.1: Class html does not exist

I am upgrading from 4.2 directly to 5.1 and run into problems with the Html and Form classes.
I followed the upgrade notes, and did
add "laravelcollective/html": "~5.0" to composer.json
composer update
add Collective\Html\HtmlServiceProvider::class to providers in app.php
add Form' => Collective\Html\FormFacade::class,
Html' => Collective\Html\HtmlFacade::class to aliases in app.php
But my views don't work. I get either Class HTML does not exist when using HTML::router or get Class html does not exist when using link_to_route
I also tried Illuminate\html instead of laravelcollective, I did a composer dump-autoload.
The complete errors:
ErrorException in Container.php line 736: Class html does not exist (View: C:\Dev\www\admin\resources\views\clubs\index.blade.php)
ReflectionException in Container.php line 736: Class html does not exist
What am I missing?
I tried everyone's answers and none of them worked for me for some reason. Ultimately I created a completely new laravel application, copied my code and then it started working, So though solved the actual problem remains a mystery.
Add in composer.json
"illuminate/html": "5.*"
and run composer update
Open your config/app.php
add under 'providers'
Illuminate\Html\HtmlServiceProvider::class,
add under 'aliases'
'Form' => Illuminate\Html\FormFacade::class,
'Html' => Illuminate\Html\HtmlFacade::class,
and under your blade templates, use as such
{!! HTML::style('assets/css/flatten.css') !!}
My solution in my case it was problem with CASE-Sensitive class name.
In my config/app.php (in aliases)
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
I am tried to use in view this code:
{!! HTML::mailto('mailto:example#example.com', 'example#example.com'); !!}
and that was an error:
"FatalErrorException in ccf70b1d0b9930d6c4e8f3859fff448f line 11: Class 'HTML' not found"
Name of class 'HTML' is CASE-Sensitive. You should use 'Html' as in your config (config/app.php) file.
Hope this help for some people.
Please change your blade file from this
{{ HTML::style('css/bootstrap.min.css') }}
to
{{ Html::style('css/bootstrap.min.css') }}
It's working.
A simple restart after composer update worked perfectly for me. I was looking for the answer, and got stuck at the same position. I'd suggest, run config:cache and cache:clear and restart the IDE. It will work.
My problem is solved, but the actual cause is still unknown. I have created a completely new laravel install and copied my source (all of it). The new application worked right away (after installing illuminate/html).
So you think I did something wrong with packages? That's what I thought, and then I did a diff on the two directories, only to find out they were identical. So it's a real mystery.
So, now everything is working, I simply renamed my new application and can continue.
I do know at some point I probably had both the collective and the illuminate versions of the HTML package installed. That's what most likely corrupted everything.
this is right way
If you try to use Form::open() or any of the Form methods in a fresh Laravel 5 install, you would get something like this:
http://laraveldaily.com/class-form-not-found-in-laravel-5/
I think I have found the solution.
In your app.php you have declared
'Form' => Illuminate\Html\FormFacade::class,
'Html' => Illuminate\Html\HtmlFacade::class,
While in your View you have called the same class as
{!! HTML::style('css/bootstrap.min.css') !!}
There is nothing wrong with the packages as the marked answer above but rather difference in capitalization of the word HTML as the previous documentation ver 5.0.*.
It should be
'Form' => Illuminate\Html\FormFacade::class,
'HTML' => Illuminate\Html\HtmlFacade::class,
Try it
php artisan cache:clear
php artisan clear-compiled
edit config/app.php
add this into providers
Collective\Html\HtmlServiceProvider::class,
and this into aliases
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,

Categories