Skip to content

Webservice

Emil edited this page Dec 9, 2024 · 16 revisions

Webservice - Connecting Javascript with PHP

The webservices are what specifies what functions the Javascript can call within PHP.

The 4 necessary files

When creating a webservice to allow Javascript to call a function in PHP 4 files are necessary to create/edit.
These files are:

  • db/services.php (edit)
  • classes/external/name_of_class.php (create)
  • amd/src/repository.js (edit)
  • amd/src/JS_file_that_calls_php_function.js (create/edit)

db/services.php

The services.php file is the file that defines what functions that the PHP should allow to be called by the Javascript.
In this file there is a list of functions that is defined, here is an example of a function:

'mod_livequiz_submit_quiz' => [
        'classname'   => 'mod_livequiz\external\submit_quiz',
        'description' => 'Record user participation and answers in a livequiz.',
        'type'        => 'write',
        'ajax'        => true,
        'services'    => [MOODLE_OFFICIAL_MOBILE_SERVICE],
    ],

We only need to edit the classname and the description. Here it is important that the classname links to the correct class in the external folder.

Clone this wiki locally