Jump to content

Return json from __execute in backend module


toni
 Share

Recommended Posts

Hi,


sorry if this might be a super easy one but I couldn't find and answer in https://github.com/processwire/processwire/blob/master/wire/core/Process.php

In a custom backend module I can just return html that gets rendered. How would I create a json endpoint that can be used from other pages of my module?

This is what I've tried

public function ___executejson() {
    return json_encode(array("a"=>"b"));
}
And from I an other page in frontend
fetch('http://localhost/pw-dev/processwire/test-list/json')
  .then(response => response.json())
  .then(commits => console.log(commits));

Obviously this is the wrong way.

Thanks a lot for your hints

Toni

 

Link to comment
Share on other sites

Wow this is fantastic! @kongondo May I ask, do you know of an example where I can see how to correctly set a hook like

I've tried within the init method but it does not what I would expect:

class ProcessMyModule extends Process {

    public function init() {
        $wire->addHook('/hello/world', function($event) {
            return 'Hello World';
        }); 
    }
...

 

Link to comment
Share on other sites

Hi @toni

Process modules is intended to be used in admin and they are not autoloaded, so for such hook you can create an satelite autoload module or put this code to init.php file.

 

class URLHooker extends WireData implements Module {
 
    public static function getModuleInfo() {
        return array(
            'title' => 'URLHooker',
            'version' => '0.1',
            'singular' => true,
			'autoload' => true,
 			'requires' => [
				'YourProcessModule>=0.1'
			],
        );
    }
 
    public function init() {
       	$this->wire()->addHook('/hello/world', function($event) {
        	return 'Hello World';
        }); 
    }
}

 

Edited by kongondo
Fixed unmatched apostrophe in code
  • Like 2
Link to comment
Share on other sites

Ok got thanks a lot to both of you, I've tested with the autoload bool but did not know that it does not work with backend
modules. Am I right that backend modules are defined by extending Process where 'satelite autoload modules' extend Module?
Thanks a lot for your patience and help!

– T

Link to comment
Share on other sites

2 hours ago, toni said:

I've tested with the autoload bool but did not know that it does not work with backend
modules

It does work with backend modules ?. The distinction in this case is not about backend or frontend. It is about autoload. Since Process modules are backend applications that you use on demand, there is no compelling reason to have them autoload. Hence, in your case the suggestion to create a module that autoloads and is dedicated to handling the URL hooks.

2 hours ago, toni said:

Am I right that backend modules are defined by extending Process where 'satelite autoload modules' extend Module?

Not really. There are different types of so-called backend modules, including Process, AdminTheme, Inputfield, etc. All modules have to implement the Module interface. In some cases they may not do it directly but do so via the parent class they are inheriting. For instance, you will find InputfieldFile extends Inputfield. Inputfield in turn extends WireData and implements Module.

Technically, there are no 'satellite' modules. This term was just used in reference to having something on the side that is not part of the main thing (hope this makes sense). So, your 'satellite' module, given that it autoloads without restrictions, is both a backend and a frontend module. You can make it autoload based on some conditions if you wish, e.g. in the backend only, or frontend only, or for some templates only, etc.

The following documentation will give you a better understanding of modules.

https://processwire.com/docs/modules/

https://processwire.com/api/ref/module/

Edited by kongondo
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...