TwoWheelDev Posted November 16, 2024 Share Posted November 16, 2024 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 More sharing options...
bernhard Posted November 16, 2024 Share Posted November 16, 2024 Have a look at URL hooks! https://processwire.com/blog/posts/pw-3.0.173/ Oh, and welcome to the forum 😉 1 Link to comment Share on other sites More sharing options...
TwoWheelDev Posted November 16, 2024 Author Share Posted November 16, 2024 Oh, that's awesome! Exactly what I was looking for! And thank you! Been registered here ages... but only just needed to actually post, such is the amount of awesome information already out there fro ProcessWire Link to comment Share on other sites More sharing options...
TwoWheelDev Posted November 28, 2024 Author Share Posted November 28, 2024 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 More sharing options...
TwoWheelDev Posted November 28, 2024 Author Share Posted November 28, 2024 (edited) 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 November 28, 2024 by TwoWheelDev Changes to code Link to comment Share on other sites More sharing options...
bernhard Posted November 28, 2024 Share Posted November 28, 2024 Looks like you could also use RockFrontend ajax endpoints: https://www.baumrock.com/en/processwire/modules/rockfrontend/docs/ajax/ Link to comment Share on other sites More sharing options...
TwoWheelDev Posted November 28, 2024 Author Share Posted November 28, 2024 Not sure, the ajax parts are actually not so much frontend pages, as just small updates (mostly around the cart system) which all has to tie-in quite closely with the module I'm building to work with a dropshipping printing api Link to comment Share on other sites More sharing options...
bernhard Posted November 29, 2024 Share Posted November 29, 2024 Adding Endpoints to 3rd party modules https://www.baumrock.com/en/processwire/modules/rockfrontend/docs/ajax/#adding-endpoints-to-3rd-party-modules 😉 But of course that means a dependency. Some might prefer that, some might not. Just wanted to mention that the option is there if anybody finds this by coincidence. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now