I want to write an application that uses parts of the mailer part of the eden library.
I have required it via composer.json:
"require": {
"eden/mail": "^1.0",
...
It seems though that the library isn't autloaded properly, as the call to the eden via eden('mail')->smtp(...) function leads to:
PHP Fatal error: Call to undefined function Kopernikus\MassMailer\Service\eden() in ~/src/massmailer/src/Kopernikus/MassMailer/Service/EdenMailerFactory.php on line 20
The quick setup guides only handles the case for the one-file approach via:
include('eden.php');
eden('debug')->output('Hello World'); //--> Hello World
I don't want to add a huge libary file and include it manually.
The autoloading seems to works fine, I just have to use the class directly instead of going with the eden() function:
use Eden\Mail\Smtp;
YourClass
{
$smtp = new Smtp(
$host,
$user,
$pass,
$port = NULL,
$ssl = false,
$tls = false
);
}
The autoloading seems to works fine basically, as I can use the class directly instead of going with the eden() function:
use Eden\Mail\Smtp;
YourClass
{
...
$smtp = new Smtp(
$host,
$user,
$pass,
$port,
$ssl,
$tls
);
}
Yet then I get weird errors like when trying to send a mail.
[Eden\Core\Exception]
Both Physical and Virtual method Eden\Mail\Smtp->_getPlainBody()
I want to go the composer route.
It seems I have to include a file via include-path option or add someting to the autoload, yet I am unsure.
How to load eden mailer component the composer way?
Not a solution to the question direclty, but I managed to circumvent the issue by switching to "nette/mail".
composer remove eden/mail
composer require nette/mail
The SMTP worked fine for me via:
$options = [
'host' => 'smtp.gmail.com',
'username' => 'YOUR_ACCOUNT',
'password' => 'YOUR_PASSWORD',
'secure' => 'ssl',
];
$mailer = new Nette\Mail\MessageSmtpMailer($options);
$message = new Nette\Mail\Message();
$message->setFrom('my.mail#gmail.com');
$message->setBody('fnord');
$message->setSubject('foo');
$message->addTo(sendTo#gmail.com);
$mailer->send($message);
From roughly looking at it, I do believe that any Eden component isn't meant to be used standalone.
Eden seems to consist of a factory function named eden(), that cannot be autoloaded (because it's a function), and that is part of the core of this framework - thus it isn't delivered by the mail component you were using. Even the tests of this component rely on the fact that the core is present, but also the SMTP class you wanted to use has a dependency that isn't resolved by default: use Eden\System\File;, as well as this: class \Eden\Mail\Smtp extends \Eden\Mail\Base together with class \Eden\Mail\Base extends \Eden\Core\Base.
This Eden framework is meant to be used either completely, or not at all. You cannot isolate components out of it.
If you are in need for a mailing library, I'd suggest https://packagist.org/packages/swiftmailer/swiftmailer. It's not a component of a framework, so you don't inherit these dependencies.
The calls like eden('debug') or eden('mail') or just shortcuts to create singletons for their appropriate Index classes. EG: eden('mail') translates to \Eden\Mail\Index();
Which results in:
eden('debug')->output('Hello World'); //--> Hello World
//or
$debug = new \Eden\Debug\Index();
$debug->output('Hello World');
Related
I am working on a project that doesn't use any framework and I would like to use Symfony Mailer component to handle sending emails.
The installation part (composer require) was well handled and everything is included in my code without any error. However, I still have a problem : the documentation of the component seems to be written only for using it with the symfony framework.
Indeed, it is refering to autoloaded config files that o
bviously don't exist in my app.
This implementation seems to be very tricky and I was wondering if any of you guys already faced the same problem and what solution you came up with?
Your question made me wonder too how it is easy to send mail only with the mailer component.
So I created a new project from scratch and tried the simplest possible version following the mailer component documentation.
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mime\Email;
class MyMailer
{
// googleDns format is gmail+smtp://USERNAME:PASSWORD#default
public function __construct(private string $googleDsn)
{
}
public function send()
{
$template = file_get_contents('https://raw.githubusercontent.com/leemunroe/responsive-html-email-template/master/email.html');
$transport = Transport::fromDsn($this->googleDsn);
$mailer = new Mailer($transport);
$email = (new Email())
->from('mygmail#address.com')
->to('thedelivery#gmail.com')
->subject('Time for Symfony Mailer!')
->html($template);
$mailer->send($email);
}
}
And I successfully received my mail. I send my mail with gmail, for your information. Transport class should do the sending job for you, but if not you can have a look to inside vendor/symfony/mailer/Transport folder
I'm not very experienced with php projects structure, I found this awesome and simple tutorial: https://arjunphp.com/creating-restful-api-slim-framework/ how to create simple slim rest app.
This is actually PHP SLIM's official project structure, my question is what is best and proper way to add and use RedBean php ORM, I dont want on every route to include something like this
use \RedBeanPHP\R as R;
R::setup( 'mysql:host=localhost;dbname=mydatabase', 'myusername', 'mypassword)
and then
$book = R::load( 'book', $id );
And then use ReadBean for my db stuff. Im wondering how to include RedBeans into project and then just use it where i need it. This is my project structure https://github.com/iarjunphp/creating-restful-api-slim-framework3.
Note: i added red beans via composer like its described here https://github.com/gabordemooij/redbean
You can put the code for setting up your libraries in any file that is going to be included on each request, so assuming you're using slim/slim-skeleton, src/dependencies.php is probably the place you want to add these two lines:
use \RedBeanPHP\R as R;
R::setup( 'mysql:host=localhost;dbname=njux_db', 'root', '');
Then you can use ReadBeans in your route callbacks but you also need to add the use \RedBeanPHP\R as R; statement to your src/routes.php as well (or any file that is going to use this class)
If you use a MVC framework (which I recommend) like codeigniter it's pretty easy.
You only have to copy your rb.php to the application/third_party folder.
Then create a file called application/libraries/rb.php containing a code like this one.
<?php
class Rb {
function __construct() {
include(APPPATH.'/config/database.php');
include(APPPATH.'/third_party/rb.php');
$host = $db[$active_group]['hostname'];
$user = $db[$active_group]['username'];
$pass = $db[$active_group]['password'];
$db = $db[$active_group]['database'];
R::setup("mysql:host=$host;dbname=$db", $user, $pass);
}
}
?>
...and vĂ´ila. RedBean will read your database configuration from CodeIgniter's standard file application/config/database.php and you will be able to use any R:: command from anywhere in your code. No includes, not additional code required :-)
i'm want to creating a design pattern and use the "Blade templating engine".
Can I use the Blade templating engine outside of Laravel and use it in my new pattern ?
For the record:
I tested many libraries to run blade outside Laravel (that i don't use) and most are poor hacks of the original library that simply copied and pasted the code and removed some dependencies yet it retains a lot of dependencies of Laravel.
So I created (for a project) an alternative for blade that its free (MIT license, i.e. close source/private code is OK) in a single file and without a single dependency of an external library. You could download the class and start using it, or you could install via composer.
https://github.com/EFTEC/BladeOne
https://packagist.org/packages/eftec/bladeone
It's 100% compatible without the Laravel's own features (extensions).
How it works:
<?php
include "lib/BladeOne/BladeOne.php";
use eftec\bladeone;
$views = __DIR__ . '/views'; // folder where is located the templates
$compiledFolder = __DIR__ . '/compiled';
$blade=new bladeone\BladeOne($views,$compiledFolder);
echo $blade->run("Test.hello", ["name" => "hola mundo"]);
?>
Another alternative is to use twig but I tested it and I don't like it. I like the syntax of Laravel that its close to ASP.NET MVC Razor.
Edit: To this date (July 2018), it's practically the only template system that supports the new features of Blade 5.6 without Laravel. ;-)
You certainly can, there are lots of standalone blade options on packagist, as long as you are comfortable with composer then there should be no issue, this one looks pretty interesting due to having a really high percentage of stars compared to downloads.
Be warned though i have not tried it myself, like you i was looking for a standalone option for my own project and came across it, i will be giving it a real good workout though at sometime in the near future,
Matt Stauffer has created a whole repository showing you how you can use various Illuminate components directly outside of Laravel. I would recommend following his example and looking at his source code.
https://github.com/mattstauffer/Torch
Here is the index.php of using Laravel Views outside of Laravel
https://github.com/mattstauffer/Torch/blob/master/components/view/index.php
You can write a custom wrapper around it so that you can call it like Laravel
use Illuminate\Container\Container;
use Illuminate\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Engines\CompilerEngine;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Engines\PhpEngine;
use Illuminate\View\Factory;
use Illuminate\View\FileViewFinder;
function view($viewName, $templateData)
{
// Configuration
// Note that you can set several directories where your templates are located
$pathsToTemplates = [__DIR__ . '/templates'];
$pathToCompiledTemplates = __DIR__ . '/compiled';
// Dependencies
$filesystem = new Filesystem;
$eventDispatcher = new Dispatcher(new Container);
// Create View Factory capable of rendering PHP and Blade templates
$viewResolver = new EngineResolver;
$bladeCompiler = new BladeCompiler($filesystem, $pathToCompiledTemplates);
$viewResolver->register('blade', function () use ($bladeCompiler) {
return new CompilerEngine($bladeCompiler);
});
$viewResolver->register('php', function () {
return new PhpEngine;
});
$viewFinder = new FileViewFinder($filesystem, $pathsToTemplates);
$viewFactory = new Factory($viewResolver, $viewFinder, $eventDispatcher);
// Render template
return $viewFactory->make($viewName, $templateData)->render();
}
You can then call this using the following
view('view.name', ['title' => 'Title', 'text' => 'This is text']);
Yes you can use it where ever you like. Just install one of the the many packages available on composer for it.
If you're interested in integrating it with codeigniter I have a blog post here outlining the process.
Following the above steps should make it obvious how to include it into any framework.
I have a Zend Framework application running on a local web server. I've run into an issue where it displays the code for certain classes. It looks like the autoloader isn't working. Whenever it tries to use a class that should have been autoloaded, it crashes saying it can't find the class, and prints the contents of the php file containing the class it was looking for.
Here's my autoloader
protected function _initAutoload()
{
echo "in autoload";
// Set up autoload.
$obj_loader = Zend_Loader_Autoloader::getInstance();
$obj_loader->setFallbackAutoloader(true);
$obj_loader->registerNamespace('Gutterbling_');
return $obj_loader;
}
The class that can't be found is Gutterbling_Acl. It doesn't say the file can't be found, just the class.
Warning : dirty quick answer.
A look in one of my Zend app and I've seen this line just before the return statement (and I don't have the call to setFallbackAutoloader) :
$obj_loader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH));
Add it and test.
Ok... Sorry to waste everyone's time. The problem was, the remote server has php short tags enabled. The local server does not. The files that aren't working start with
Again, sorry about that. Hopefully this helps someone with the same problem.
I'm pretty much newbie in Zend Framework action helpers and I am trying to use them with no success (I read a bunch of posts about action helpers, including http://devzone.zend.com/article/3350 and found no solution in like 8 hours). I used Zend Tool to setup my project and the name of the helper is Action_Helper_Common. No matter what I do, I get following error "Fatal error: Class 'Action_Helper_Common' not found". Currently, I have things set up like this:
zf version: 1.11.3
helper name: Action_Helper_Common
helpers location:
/application/controllers/helpers/Common.php
In Bootstrap.php i have following function:
protected function _initActionHelpers() {
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH . '/controllers/helpers', 'Action_Helper');
Zend_Controller_Action_HelperBroker::addHelper(
new Action_Helper_Common(null, $session)
);
}
I also tried this without success (it was defined in Bootstrap.php before _initActionHelpers):
protected function _initAutoloader() {
$moduleLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH . '/controllers/helpers'));
return $moduleLoader;
}
So what am I doing wrong?!?! PLZ help, I am desperate and about to give up :)
You got error because you haven't setup autoloader for Action_Helper_*
Resource autoloader
Helper broker uses plugin loader to load helpers based on paths and prefixes you specified to it. That is why ::getHelper() can find your helper
you dont need to do (so remove it)
Zend_Controller_Action_HelperBroker::addHelper(
new Action_Helper_Common(null, $session)
); ,
since you already did
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH . '/controllers/helpers', 'Action_Helper');
when you will do
$myhelper = $this->getHelper('Common');
in your controller zf will lookinto directory /controllers/helpers/ for class name Action_Helper_Common and create an instance for you and return it.
For some reason the following line didn't work for me as well:
Zend_Controller_Action_HelperBroker::addHelper( new Action_Helper_Common() );
I just keep getting a 'Class not found' error each time I'm creating a new helper object explicitly.
This is what works for me:
Zend_Controller_Action_HelperBroker::getHelper('Common');
In this case, new Action_Helper_Common object gets created and is registered with Helper Broker.
Not sure though if it works for you, since you have a parameterized constructor.