Loading...
 
Skip to main content

History: Create a service

Source of version: 7 (current)

Copy to clipboard
!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 +-.

{CODE()}
bash$ mkdir lib/core/Services/Hello
{CODE}


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

{CODE(colors="php")}
class Services_Hello_Controller
{
    function action_say($input) {
        return array('hello world');
    }
}
{CODE}


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.
{CODE(colors="xml")}
...
		<service id="tiki.controller.hello" class="Services_Hello_Controller"/>
	</services>
</container>
{CODE}


4) Remove your cache files.
{CODE()}
find temp/cache/ -type f -not -name index.php -delete
{CODE}


5) Check if everything is working. You can try with -+ curl +-
{CODE()}
curl "http://localhost/tiki-ajax_services.php?controller=hello&action=say"
{CODE}

!! Notes

# The controller class name has to start with -+Services_+-. 
** Right: -+Services_Hello_Controller+-
** Wrong: -+Service_Hello_Controller+-
# 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.

{CODE(colors="diff")}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');
+    }
+}
{CODE}
Show PHP error messages