4

I have website created in Joomla and Jomsocial.

I am creating web services/API in Joomla for my mobile app to access.

The mobile app will have almost all same functionality as like website.

Can I access functions(tasks) defined inside controller/model externally in my web services/API? How?

Note: I am using phonegap to develop mobile apps

Amol Chakane
  • 1,501
  • 2
  • 21
  • 43

2 Answers2

3

You cannot invoke directly a specific function in a a model or a view.

But any public methods without parameters in the controller will be invoked with

yoursite/index.php?option=com_yourcomponent&task=yourpublicmethod.

This will trigger the specified method and render it within Joomla template, with all relevant modules etc.

If you only want the main component output, add to the call

&tmpl=component

which renders through the template's component.php instead of index.php, usually the former only loads the external resources (css and js) and the main component output.

Else if the function returns code (either html xml or json) you may want to invoke it with

&format=raw

which returns just the bare output of yourpublicmethod. This can also be achieved at the component level with the instruction

exit;

at the end of the implementation of yourpublicmethod

Riccardo Zorn
  • 5,590
  • 1
  • 20
  • 36
  • Tried the way you suggested. But it doesn't work. Can I access Jomsocial's inbuilt tasks by including that specific file in my API? – Amol Chakane Jan 29 '13 at 09:04
  • most likely, I have done so in a Joomla module, you just need to bootstrap jomsocial (just copy one of the bundled modules to see how!) – Riccardo Zorn Jan 30 '13 at 22:57
0

Joomla CMS implements the MVC Patter (module-view-controlle), with a small difference of the view. It implements the view-model view.

In Joomla everything is a component. Each component can have its out models, views, controllers, db tables, etc.

Easy example how it works is you create a component lets say "helloworld" component and this component resides in JROOT/administrator/components/com_helloworld for the admin panel part of the component and JROOT/components/com_helloworld for the front-end.

In those directories you have one file called com_helloworld.php which triggers the entire MVC for this request.

Although Joomla implements the Front controller pattern you can build you component however suits you best (with or without front controller).

When you request http://domainname.com/index.php?option=com_helloworld, the file that I mentioned com_helloworld gets called and everything inside executes.

For more info see this veeery helpful article about developing Joomla CMS 2.5 components:

http://docs.joomla.org/Developing_a_Model-View-Controller_Component/2.5/Introduction

Hope this helps :)

Stoyan Dimov
  • 5,250
  • 2
  • 28
  • 44
  • I have developed components in joomla. With respect to your example say com_helloworld component has controller and model in which there are some task named sayhello(). I want to call this sayhello task in my web service/API. Is it possible? – Amol Chakane Jan 28 '13 at 11:13
  • Yes, you accomplish that with: http://domainname.com/index.php?option=com_helloworld&task=controllername.methodname Where controllername is the name of the controller in the component and methodname is the name of the method inside the controller. – Stoyan Dimov Jan 28 '13 at 14:04
  • OK I will give it a try. But can I call this task using jQuery.ajax? – Amol Chakane Jan 29 '13 at 06:33
  • I am using Advanced REST Client app in chrome browser to call this task. But it doesn't work. I use phonegap to develop mobile app. So I want to call this task using jQuery.ajax. Any ideas? – Amol Chakane Jan 29 '13 at 07:19
  • Yes you can, you need to create a separate method and on top of it disable the layout (as in most frameworks). Be careful though cause Joomla uses Motools by default. So you need the JQuery.noConflict(). – Stoyan Dimov Jan 29 '13 at 11:37
  • What did you do? And how did you find out that it doesn't work? – Stoyan Dimov Jan 29 '13 at 12:51
  • I tried below url in Google chrome's Advanced REST client app **http://www.mydomain.com/?option=com_community&task=photos.removealbum**. Passed required params too. I didn't get where do I have to create a seperate method? in controller/model itself? Because, I am creating REST APIs, extending APIResource class in joomla. – Amol Chakane Jan 30 '13 at 05:08
  • The problem is that you need to register the component with Joomla. You can do this via a db entry but that is complicated. Based on your last comment I created a small component "community" with a controller "photos" that will execute with the link that you have stated. When you install this archive it will register it appropriate. If you give me your email I'll send you the component including explanation how to install it and edit the controller. – Stoyan Dimov Jan 30 '13 at 08:45