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
.
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
- 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.
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');
+ }
+}
!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}