How to create a new service
To create a very basic service you will need:
- add folder with camel-case name of your service in
lib/core/Services
- create a php file with a Controller class in that folder
- 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
.
bash$ mkdir lib/core/Services/Hello
2) Add the controller file called lib/core/Services/Hello/Controller.php
, as below
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.
... <service id="tiki.controller.hello" class="Services_Hello_Controller"/> </services> </container>
4) Remove your cache files.
find temp/cache/ -type f -not -name index.php -delete
5) Check if everything is working. You can try with curl
curl "http://localhost/tiki-ajax_services.php?controller=hello&action=say"
Notes
- The controller class name has to start with
Services_
.- Right:
Services_Hello_Controller
- Wrong:
Service_Hello_Controller
- Right:
- The service id ends with folder name, but in lower case, like
tiki.controller.hello
- 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.
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'); + } +}