PHP code example of contenir / contenir-audience-mezzio

1. Go to this page and download the library: Download contenir/contenir-audience-mezzio library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

contenir / contenir-audience-mezzio example snippets


new PhpFileProvider(realpath(__DIR__) . '/autoload/{{,*.}global,{,*.}local}.php'),
new PhpFileProvider(realpath(__DIR__) . '/autoload/extensions/{{,*.}global,{,*.}local}.php'),

// config/autoload/extensions/campaign-monitor.global.php
return ['extensions' => ['campaign-monitor' => ['bindings' => [
    'newsletter' => [
        'list'    => 'a1b2c3',
        'fields'  => ['Name' => 'name'],
        'consent' => 'Signing up to the newsletter',
        'options' => ['consent_to_track' => 'Yes'],
    ],
]]]];

// config/autoload/extensions/campaign-monitor.local.php, not committed
return ['extensions' => ['campaign-monitor' => ['pepper' => '…the add-on\'s pepper…']]];

// config/autoload/audience.global.php
return [
    'contenir_audience' => [
        'handle'  => 'campaign-monitor',             // the add-on: its tables are ext_campaign_monitor_*
        'adapter' => PhpDb\Adapter\Adapter::class,   // the site's database; default AdapterInterface::class
        'clock'   => null,                           // a PSR-20 clock service; default the system's time
    ],
];

use Contenir\Audience\Exception\StorageException;
use Contenir\Audience\Mezzio\Subscriptions;

final class SubscribeHandler implements RequestHandlerInterface
{
    public function __construct(private Subscriptions $subscriptions, /* … */) {}

    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        // … validate the form …
        try {
            $this->subscriptions->subscribe(
                'newsletter',
                $data['email'],
                ['name' => $data['name'] ?? ''],
                ip: $request->getServerParams()['REMOTE_ADDR'] ?? null,
            );
        } catch (StorageException) {
            // the outbox row couldn't be written: the one failure to show the visitor
        }

        // … render "thanks" …
    }
}