Jump to content

Handling a callback from an API


Recommended Posts

I'm in the process of developing a module, that will need to handle callbacks from the third-party API.

The pages for the callback will need to be accessible from frontend without a login, what's the best way to handle this from a module. Is it possible to do without manually creating the pages... I guess some sort of registering a URL that processwire will send to the module to do the processing

Link to comment
Share on other sites

  • 2 weeks later...

So I've got the URL Hooks working wonderfully! makes it really easy to create some custom logic to handle things

At the moment, I'm using a mixture of urlSegments (for one route e.g. /shop) and urlHooks (for the backend json stuff at /api/<stuff here>).

Is it possible to use the urlHooks to create a something similar to the urlSegments. I see in the documentation you can return a page, but is it possible to create the hooks function that would get a template and do the necessary before returning that to the user? I guess something similar to the way the an admin module can use views for the pages, and the function just pushes the data into that page.

Hopefully that makes sense! 🤔

Link to comment
Share on other sites

Time to answer my own question... and love it or hate it AI came up with the goods on this one!

Defining the urlHook as follows:

public function init() {
    $this->addHook('/custom-url/', $this, 'handleCustomURL');
}

And the handleCustomURL function (this was the second iteration I got for this to include having a default template in the module, whilst allowing them to be overridden in the site/templates/ folder):

public function handleCustomURL(HookEvent $event) {
    // Path to the override template in the site's templates directory
    $overrideTemplatePath = $this->config->paths->templates . 'custom-template.php';

    // Path to the default template in the module's directory
    $defaultTemplatePath = $this->config->paths->siteModules . $this->className() . '/templates/custom-template.php';

    // Determine which template to use
    $templatePath = file_exists($overrideTemplatePath) ? $overrideTemplatePath : $defaultTemplatePath;

    // Check if the determined template exists (for safety)
    if (!file_exists($templatePath)) {
        throw new Wire404Exception("Template not found");
    }

    // Render the template and pass variables
    $output = $this->files->render($templatePath, [
        'someVariable' => 'someValue',
        // Add additional data if needed
    ]);

    // Output the rendered content
    echo $output;

    // Stop further processing
    $event->replace = true;
}

I did make a small to change to the end of the function as follows:

    // Output the rendered content
    //echo $output;
	return $output;

    // Stop further processing - Doesn't look like this is needed!
    // $event->replace = true;
}

Not sure any amount of Google-Fu would have got me to this, so it's interesting to see how AI can pull the correct bits of information together! I have obviously tested the above code, and it seems to work a treat!

Edited by TwoWheelDev
Changes to code
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...