3

I am totally Joomla noob and trying to figure out how to control MVC in Joomla. I've read this "Developing a MVC Component" in Joomla basic tutorial and understand that the main controller is located under components/com_component/controller.php & other file is components/com_helloworld/helloworld.php which is pointing to controller.php file to execute the request. The question is, I want to create new controller folder and put all controllers in that folder like below. How to do that? Controllers folder

Any advise is really appreciated. Thank you so much!!

7

I think you're confusing the purpose of your main / master controller (controller.php) and subcontrollers (everything in the /controllers folder). For more on this, see the docs.

The main controller is typically used to do what you mention in your comment (render a view based on the view parameter, usually specified in the URL). In controller.php, the function that does this is called display(). So the following URL:

http://example.com/index.php?option=com_mycomponent&view=test

Will result in calling the display() function in controller.php and loads the view test in /views/test.

Subcontrollers, in the controllers folder, are generally used for CRUD tasks, but can be called easily by using a task URL parameter. For example:

http://example.com/index.php?option=com_mycomponent&task=test.process

Will result in calling the process() function in /controllers/test.php - notice the task parameter is <controllerName>.<functionName>

You'll see this used a lot to do actions that don't need a view, e.g in com_content to publish / checkin / checkout / delete articles (where the task is article.publish etc.,which means the controller at controllers/article.php and the function publish(), and instead of a dedicated view the controller just redirects back to the list view).

For more information on how all the Joomla MVC parts fit together, see this wiki article.

Good luck!

  • Thanks for details explanation about sub controllers, the docs are really poor and confuse. Which took me long time to understand the concept of Joomla :(. – SonDang Jun 4 '15 at 2:45
  • It can be a little tricky, but it makes sense soon enough :) – codinghands Jun 4 '15 at 9:28
2

Joomla MVC follows a naming convention to choose the file and class names.

This folder holds additional controllers, if needed by the application.

   /site/controllers/{controllername}.php
   This file holds the controller class {ComponentName}Controller{ControllerName}. This class must extend the base class JController.

You find more information in Joomla Docs, here

  • Hi Anibal, thanks for the answer. But the docs are not clear to me. Suppose I have "/site/controllers/test.php" controller. In "/site/" I have "router.php" & "controller.php", how do I use test.php controller to render the view "test" :) ? – SonDang May 31 '15 at 9:24
  • Joomla MVC has a convention to load certain files and execute classes and function. That's the point of the whole MVC pattern. – Anibal May 31 '15 at 12:09
1

If by all controllers you mean everything but controller.php, then placing them in that folder will work just fine. You call a specific controller with the task variable. For instance if you want to call the save method of the employer controller then you set the task to employer.save

I hope this helps. Happy Joomla!ng

  • Hi Mathew, thanks for help. It's quite confuse with me, could you send me some demo please?? – SonDang May 31 '15 at 9:26
  • You can check how any core controller works, for example com_contact. – Anibal May 31 '15 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.