Loading...
 
Skip to main content

Create a service

How to create a new service


To create a very basic service you will need:

  1. add folder with camel-case name of your service in lib/core/Services
  2. create a php file with a Controller class in that folder
  3. modify file db/config/controllers.xml and add your controller


Example


As an example, lets create a service to output a simple json response. The service name will be Hello.


1) Create the folder lib/core/Services/Hello .

Copy to clipboard
bash$ mkdir lib/core/Services/Hello



2) Add the controller file called lib/core/Services/Hello/Controller.php , as below

Copy to clipboard
class Services_Hello_Controller { function action_say($input) { return array('hello world'); } }



3) Edit db/config/controllers.xml and reference your new controller. If you add the reference at the end of file, it will looks like this.

Copy to clipboard
... <service id="tiki.controller.hello" class="Services_Hello_Controller"/> </services> </container>



4) Remove your cache files.

Copy to clipboard
find temp/cache/ -type f -not -name index.php -delete



5) Check if everything is working. You can try with curl

Copy to clipboard
curl "http://localhost/tiki-ajax_services.php?controller=hello&action=say"

Notes

  1. The controller class name has to start with Services_.
    • Right: Services_Hello_Controller
    • Wrong: Service_Hello_Controller
  2. The service id ends with folder name, but in lower case, like tiki.controller.hello
  3. If you think your code is right but it throws error, try cleaning you cache like step 4

Appendix


Here a patch file you can apply in you tiki and test this example.

Copy to clipboard
Index: db/config/controllers.xml =================================================================== --- db/config/controllers.xml (revision 61445) +++ db/config/controllers.xml (working copy) @@ -69,5 +69,6 @@ <service id="tiki.controller.wiki" class="Services_Wiki_Controller"/> <service id="tiki.controller.wiki_structure" class="Services_Wiki_StructureController"/> <service id="tiki.controller.workspace" class="Services_Workspace_Controller"/> + <service id="tiki.controller.hello" class="Services_Hello_Controller"/> </services> </container> Index: lib/core/Services/Hello/Controller.php =================================================================== --- lib/core/Services/Hello/Controller.php (nonexistent) +++ lib/core/Services/Hello/Controller.php (working copy) @@ -0,0 +1,8 @@ +<?php + +class Services_Hello_Controller +{ + function action_say($input) { + return array('hello world'); + } +}
Collapse/expand modules below
Show PHP error messages