Silex traits for swiftmailer. Fatal error: Call to undefined method Silex\Application::mail() - swiftmailer

I am trying to use traits in Silex for Swift mailer.
I have included:
use Silex\Application\SwiftmailerTrait;
I also checked that traits file were in the right Silex vendor directory.
Test traits:
$app->mail(\Swift_Message::newInstance()
->setSubject("title")
->setFrom(["www.domain.com"]])
->setTo(["something#domain.com"])
->setReplyTo(["user.email#some.com"])
->setBody("TEST MESSAGE")
);
Then, I get this error message:
Fatal error: Call to undefined method Silex\Application::mail() in
...\app.php on line 88
Just to make it clear. I can, without any issue, use standard way of using swift in Silex and it works just fine.
This is the working bit without traits:
// $message = \Swift_Message::newInstance()
// ->setSubject('[YourSite] Feedback')
// ->setFrom(array('noreply#yoursite.com'))
// ->setTo(array('feedback#yoursite.com'))
// ->setBody($request->get('message'));
// $app['mailer']->send($message);
However I was wondering what was actually stopping Silex from running swift with traits. Any idea ?
I am using PHP Version 5.6.11
My composer file:
{
"require": {
"components/jquery": "^2.2",
"components/css-reset": "^2.5",
"silex/silex": "~1.2",
"symfony/browser-kit": "~2.3",
"symfony/console": "~2.3",
"symfony/config": "~2.3",
"symfony/css-selector": "~2.3",
"symfony/dom-crawler": "~2.3",
"symfony/filesystem": "~2.3",
"symfony/finder": "~2.3",
"symfony/form": "~2.3",
"symfony/locale": "~2.3",
"symfony/process": "~2.3",
"symfony/security": "~2.3",
"symfony/serializer": "~2.3",
"symfony/translation": "~2.3",
"symfony/validator": "~2.3",
"symfony/monolog-bridge": "~2.3",
"symfony/twig-bridge": "~2.3",
"doctrine/dbal": ">=2.2.0,<2.4.0-dev",
"swiftmailer/swiftmailer": "5.*",
"twig/twig": "^1.24",
"symfony/security-csrf": "~2.3",
"symfony/yaml": "~2.3"
},
"autoload": {
"psr-4": {
"WL\\Form\\": "WL/Form/",
"WL\\Email\\": "WL/Email/"
},
"classmap":[],
"files":[]
}
}

You need to create a custom Application class which extends \Silex\Application and uses that trait.
Assuming a base project tree as:
project/
|
|_app/
|
|_src/
|
|_vendor/
|
|_web/
You need a class definition:
// src/WL/App.php
namespace WL;
class App extends \Silex\Application
{
use \Silex\Application\SwiftmailerTrait;
// add some other trait
// even custom methods or traits
}
A bootstrap :
// app/bootstrap.php
$app = new \WL\App();
// configure it, register controllers and services, ...
// or import them
foreach (glob(__DIR__ . "/../src/WL/Controller/*.php") as $controllers_provider) {
include_once $controllers_provider;
}
return $app;
So you can import a controller collection like :
// src/Wl/Controller/blog.php
use Symfony\Component\HttpFoundation\Request;
/** #var \Silex\ControllerCollection $blog */
$blog = $app['controllers_factory'];
// define some routes
$blog->post('/send-mail', function (Request $request, \WL\App $app)
{
// Now this application passed to your controller is an
// instance of custom \App which has the trait you want
// in contrary with the default \Silex\Application
$app->mail(...
}
$app->mount('/blog', $blog);
And a front controller :
// web/index.php
// define autoloading
// customize debug and server parameters
$app = require_once '../app/bootstrap.php';
$app->run();

Related

use Laravel Excel inside laravel package development

I'm creating my own Laravel package for the first time. I create a new project and require orchestra/testbench in the project. Things look okay and I'm able to run tests inside the package but I couldn't use Laravel Excel inside my package.
in composer.json I added
"extra": {
"laravel": {
"providers": [
"Maatwebsite\\Excel\\ExcelServiceProvider"
],
"aliases": {
"Excel": "Maatwebsite\\Excel\\Facades\\Excel",
}
}
},
"require-dev": {
"orchestra/testbench": "6.0",
"phpunit/phpunit": "^9.5",
"maatwebsite/excel": "^3.1"
}
And also ran composer dump-autoload, when I want to use Laravel Excel inside my package I tried
use Maatwebsite\Excel\Facades\Excel;
class TaxCalculation {
public function incomeTax(): float
{
$table = Excel::import(new TaxImport(), 'file.xls');
}
But got an error
Illuminate\Contracts\Container\BindingResolutionException: Target class [excel] does not exist.
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Container\Container.php:832
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Container\Container.php:712
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:796
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Container\Container.php:651
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:781
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Container\Container.php:1354
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:198
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:166
D:\code\packages\thai_tax\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:255
D:\code\packages\thai_tax\src\Services\TaxCalculation.php:45
D:\code\packages\thai_tax\tests\Unit\CreateFacadeTest.php:26
Caused by
ReflectionException: Class excel does not exist
Excel does not seem to load. Is there any more steps I need to do to use it?

laravel owen-it/laravel-auditing not update db

I'm working on laravel project and it uses caffeinated/modules. For a reason i need to track history of model value changes. So I tried owen-it/laravel-auditing(https://packalyst.com/packages/package/owen-it/laravel-auditing) package. Migrations created audit table. But when i update a model which is applied owen-it/laravel-auditing it's not updating the audit table.
my model is
namespace App;
use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Contracts\Auditable;
class BillingProgramme extends Model implements Auditable
{
use \OwenIt\Auditing\Auditable;
}
my composer.json
"require": {
"php": "^7.1.3",
"caffeinated/modules": "^4.3",
"guzzlehttp/guzzle": "^6.2",
"h4cc/wkhtmltopdf-amd64": "^0.12.4",
"laravel/dusk": "^5.0",
"laravel/framework": "5.7.*",
"laravel/telescope": "~1.0",
"laravel/tinker": "^1.0",
"moontoast/math": "^1.1",
"owen-it/laravel-auditing": "^8.0.0",
"paulvl/backup": "3.*",
"phpoffice/phpspreadsheet": "^1.11",
"tymon/jwt-auth": "1.0.0-rc.3",
"wildside/userstamps": "^0.5.5"
},
if anyone note the issue please give me the solution.
I use this library as well and works well for me like this;
use OwenIt\Auditing\Contracts\Auditable as AuditableContract;
class ClassName extends Model implements AuditableContract{
use Auditable;
/**
* Attributes to include in the Audit.
*
* #var array
*/
protected $auditInclude = [
'WHATEVER YOU WANT TO AUDIT',
];
}

Symfony 4 deployment not working

I'm trying to deploy a symfony 4 project in my server, but I'm having problems to acomplish it production (in local it works perfect).
Previously I have deployed a lot of symfony 3 projects in my server, so I decided to test a Symfony 4.
This is the error that I'm getting in my log:
[Wed Dec 06 10:26:33.141952 2017] [:error] [pid 13485] [client 84.120.210.249:50394] PHP Fatal error: Uncaught TypeError: Return value of Symfony\Component\Dotenv\Dotenv::populate() must be an instance of Symfony\Component\Dotenv\void, none returned in /var/www/my_project/vendor/symfony/dotenv/Dotenv.php:95\nStack trace:\n#0 /var/www/my_project/vendor/symfony/dotenv/Dotenv.php(57): Symfony\Component\Dotenv\Dotenv->populate(Array)\n#1 /var/www/my_project/public/index.php(15): Symfony\Component\Dotenv\Dotenv->load('/var/www/rodage...')\n#2 {main}\n thrown in /var/www/my_project/vendor/symfony/dotenv/Dotenv.php on line 95
This is my index.php (default index.php that you get when you create a project):
<?php
use App\Kernel;
use Symfony\Component\Debug\Debug;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpFoundation\Request;
require __DIR__.'/../vendor/autoload.php';
// The check is to ensure we don't use .env in production
if (!isset($_SERVER['APP_ENV'])) {
if (!class_exists(Dotenv::class)) {
throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
}
(new Dotenv())->load(__DIR__.'/../.env');
}
if ($_SERVER['APP_DEBUG'] ?? ('prod' !== ($_SERVER['APP_ENV'] ?? 'dev'))) {
umask(0000);
Debug::enable();
}
if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? false) {
Request::setTrustedProxies(explode(',', $trustedProxies), Request::HEADER_X_FORWARDED_ALL ^ Request::HEADER_X_FORWARDED_HOST);
}
if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? false) {
Request::setTrustedHosts(explode(',', $trustedHosts));
}
$kernel = new Kernel($_SERVER['APP_ENV'] ?? 'dev', $_SERVER['APP_DEBUG'] ?? ('prod' !== ($_SERVER['APP_ENV'] ?? 'dev')));
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
This is what I have in my composer.json:
"require": {
"php": "^7.1.3",
"symfony/asset": "^4.0",
"symfony/console": "^4.0",
"symfony/dotenv": "^4.0",
"symfony/flex": "^1.0",
"symfony/framework-bundle": "^4.0",
"symfony/lts": "^4#dev",
"symfony/twig-bundle": "^4.0",
"symfony/web-server-bundle": "^4.0",
"symfony/yaml": "^4.0"
},
These are the things that I tried:
-cache:clear
-composer update
-apache restart
I think that it something about environment variables, but not really sure...
Any suggestions?
Thank you all for your time!
Void returns are only available from PHP 7.1. My guess is that you are using an earlier version of PHP.
Look at this answer

Class dompdf.wrapper does not exist laravel 4.2.0

I am trying to use the dompdf package with Lavarel 4.2.0 in my project authored by barryvdh, I tried installing three different versions of this package (0.6*, 0.6.1,0.5.2)
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"laravel/framework": "4.2.*",
"fzaninotto/faker": "^1.6",
"barryvdh/laravel-dompdf": "0.5.2"
How ever each time I try to create a PDF file using
Route::get('/invoice', function()
{
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1>');
return $pdf->stream();
});
I get this error
Class dompdf.wrapper does not exist (vendor\laravel\framework\src\Illuminate\Container\Container.php)
/ hand back the results of the functions, which allows functions to be
// used as resolvers for more fine-tuned resolution of these objects.
if ($concrete instanceof Closure)
{
return $concrete($this, $parameters);
}
$reflector = new ReflectionClass($concrete);
// If the type is not instantiable, the developer is attempting to resolve
According to the docs, we need to install the 0.4.x versions for Laravel 4. Your project is using versions for Laravel 5.
composer.json:
"require": {
...
"barryvdh/laravel-dompdf": "0.4.*"
}
To resolve the dompdf service from the container, use 'dompdf', not 'dompdf.wrapper':
$pdf = App::make('dompdf');
Or, use the PDF facade:
use PDF;
...
$pdf = PDF::loadHTML('<h1>Test</h1>');
Be sure to register the service provider and facade in config/app.php.
I put in file config/app.php into providers section:
Barryvdh\DomPDF\ServiceProvider::class,
And into aliases section:
'PDF' => Barryvdh\DomPDF\Facade::class,
And I use this in my controller:
use PDF;
...
$pdf = PDF::loadView('panel.cooperation-offers.pdf', $showData);
Here is my composer.json:
...
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.2.*",
"barryvdh/laravel-dompdf": "0.6.*"
}, ...

Error running make:request on laravel 5

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"

Resources