XF 2.0 User Registration Event

BoostN

Well-known member
I'm diving into the code here for XF2.

If I'm wanting to call an event, say using GuzzleHTTP to post to some API that a registration event has happen.. which class would I extend?

XF\Pub\Controller\Register
or
XF\Service\User\Registration

..or am I totally off?
 
Generally the Service classes are more flexible and clear and geared towards being extended moreso than the controller so I would look at extending XF\Service\User\Registration and injecting your custom behaviours there.
 
Now I'm looking at Code Event Listeners. If my addon doesn't actually modify data, do I still need one? I have a bare bones template built out, but I'm not sure what to put inside my function if I'm not actually modifying data. The examples from the DEV Docs really only shows you modifying data within both the forum and thread entity.

I'm just calling a Service I've built within my add-on, like so:

Service: (I'm using cURL right now as I don't want to get to involved trying to call Guzzle while trying to figure out the XF inner workings)

PHP:
<?php

namespace BoostN\Slack\Service;

use XF\Service\AbstractService;

class SlackNotify extends AbstractService
{
    public function postToSlack(array $payload)
    {
        $app = \XF::app();
        
        $epayload = json_encode($payload);
        
        $webHook = $app->options()->boostnSlackwebHook;

        $ch = curl_init($webHook);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $epayload);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $response = curl_exec ($ch); // execute
        curl_close ($ch); // close curl handle
        return $response;
    }

}

Class Extension:

PHP:
<?php

namespace BoostN\Slack\XF\Service\User;

class Registration extends XFCP_Registration
{
    protected function notifySlack()
    {
        parent::sendRegistrationContact();

        $app = \XF::app();

        $payload = array (
            'attachments' =>
                array (
                    0 =>
                        array (
                            'text' => 'Something is broken',
                            'title' => 'From Registration.php Class Ext',
                            'title_link' => 'https://google.com',
                        ),
                ),
            'channel' => '#general',
            'icon_emoji' => ':exclamation:',
            'icon_url' => NULL,
            'text' => 'Test from XenForo',
            'username' => $app->options()->boostnSlackuserName,
        );

        if($app->options()->boostnSlackenableNewUserReg)
        {
            /** @var \BoostN\Slack\Service\SlackNotify $slackService */
            $slackService = $this->service('BoostN\Slack:SlackNotify');
            $slackService->postToSlack($payload);
        }
    }
}

And then my Extension:
1503023030521.webp

I have a feeling the code event I need to work with is the user_content_change_init and then call the method within my Listener, but I'm not sure why.

Sorry for all the questions, I'm trying to learn and get my feet wet a little bit.
 
The class extension is what you need here; you shouldn't need a listener. Mostly it looks correct, except you have this line in your service extension:
Code:
protected function notifySlack()
You are trying to trigger extra behavior when the registration contact is sent, so that should be:
Code:
protected function sendRegistrationContact()

Strictly speaking, you don't need your SlackNotify service as something separate. You can move your postToSlack() method into your registration extension if you prefer and just call it there.
 
Back
Top Bottom