Global require... e.g. composer, namespaces

i just updated my phpmailer installation via composer and previously everything worked fine.

just now i noticed phpmailer is using namespaces so i had to make some chances.

i used to require vendor/autoload.php just within a plugin and it seemed to be working fine for the most part. however with the namespaces, i could not seem to get it working via a kirby hook function.

i have tried several places but couldn’t decide where i should require autoload to make it most accessable everywhere. while controllers/templates work, plugins do seem to have an issue.

<?php
use PHPMailer\PHPMailer\PHPMailer;
$kirby = kirby();
$site  = site();
$user  = site()->user();
$users = site()->users();

kirby()->hook('panel.page.update', function($page) {
// Use Email Function below ...................

$mail = new PHPMailer;
// WIth the line above, it'll say class 'PHPMailer\PHPMailer\PHPMailer' not found in file: (this file)

im not using PHPMailer but i do have a site that uses third party libraries installed via Composer (specifically, its the google calender API for PHP library.)

All i did was require this library at the top of all the template files (for some reason a Kirby snippet call broke it, but a require works.):

<?php require_once 'site/snippets/site-caldata.php'; ?>

The snippet contains this at the top

<?php
require_once 'vendor/autoload.php';
// My custom code.....

Does that help at all?

1 Like

template won’t help me, as i am calling that specific library from within a plugin with no template attached.

all templates and controllers seem to work fine for me though. my approach there is to either

// config.php
include_once('vendor/autoload.php');

or

// site/plugins/composer.php
include_once('vendor/autoload.php');

seems to work both, no need to add to each template this way… but calling it from another plugin as i said, does not seem to work for me.

Im probably going to get into trouble for this because its a hack, but what happens if you require the library into Kirbies index.php. I’m not hugely knowledgable about PHP but i think that will technically make it available eveywhere.

who says this is a hack?

isn’t it the point of composer to make a library available thoughout the whole project?

it’s not like anyone could just access it…
so to clarify, each file which needs to use phpmailer in this case, would have a separate line to require autoload?

so having 20 templates/controllers which send email though php, mean 20 times manually adding the require… meh.

I am because I am suggesting altering Kirbies default index.php. I don’t mean what your doing is a hack. :slight_smile:

ah, your suggestion of altering index.php was “a hack”… i thought you were saying using a plugin would be a hack in your eyes … :rofl:

i am happy to hear other comments about best practices with composer and kirby…

It must be doable because i have seen plugins that are installable via composer.

I’d create a site.php.

<?php
use PHPMailer\PHPMailer\PHPMailer;
require(__DIR__ . DS . 'vendor' . DS . 'autoload.php');
$kirby = kirby();

Then in your plugin:

<?php
kirby()->hook('panel.page.update', function ($page, $oldpage) {
  $mail = new \PHPMailer\PHPMailer\PHPMailer();
});

so @texnixe’s suggestion is partly a solution.

// site.php
<?php
require(__DIR__ . DS . 'vendor' . DS . 'autoload.php');
$kirby = kirby();

No need to put “use Namespace” there, it seems that it’s only limited within the same file each, so in each template or controller / plugin i’d have to post it yet again…

<?php
// controller.php
use PHPMailer\PHPMailer\PHPMailer;
return function($site, $pages, $page){
// here it'S important to declare it before any function starts.
// template.php
use PHPMailer\PHPMailer\PHPMailer;
// do more
// plugin
use PHPMailer\PHPMailer\PHPMailer;
// do more

i have seen it’s getting more and more common to use namespaces, e.g. paypal ipn also suggests that on github, so for reference this would be similar.

Would be nice if this would be part a section in kirby’s plugin docs!

@texnixe

@afincato This is Composer specific, see the Composer documentation (Autoloading)