0

I'm a developer and I am very confident with MVC pattern and have already developed a lot of webapp from scratch using php framework like symfony or yii.

I'm a little bit confused about joomla mvc and terminology and after googling a lot, read joomla book extensions guide, read on joomla website my doubt are still there.

What is confusing for me is the component mvc structure and how I have to set up "my way of think" about joomla mvc, for doing the things in the joomla way.

In particular I am used to reasoning in terms of controller/action (like in symfony and yii framework)

So the final list of all my webapp url will be

 controller1/action1
 controller1/action2
 controller1/action3

 controller2/action1
 controller2/action2

Each controller's action will decide what view to render and what layout to use for showing the view. In particular in these frameworks, the definition of a layout is exactly the mean of a layout. And the view is the "core part" of the page.

So I can have a view with a list of users and I can put this view inside a mobile layout or a desktop layout, or to build a view for mobile and put it only in the mobile layout and so on.

The final result about directory structure in my webapp is something similar to the following:

 controllers/
    controller1
    controller2
    controller3
models/
    modelForTableA
    modelForTableB
views/
   controller1/
     viewForAction1
     viewForAction2
layouts/
    mobileLayout.php
    desktopLayout.php

and for me is very clear to understand.

So finally my questions are:

  • how would be the directory structure in joomla?
  • what are in joomla the definition of view, layout and task?

I need to clarify that I do not need an explaination about MVC works in general, but if I would achieve the same result as before, how I have to organize my code and my work?

Suppose that I want to build a component with the following "url"

userController/addUser
userController/editUser
userController/listUsers
userController/viewUserDetail

anotherController/addOperation
anotherController/editOperation
anotherController/myNonCrudOperation

Thank you very much

user2548436
  • 915
  • 2
  • 16
  • 35

3 Answers3

2

Routing in Joomla is slightly different. The SEF URLs are built from menu items, which in turn point to a View/Layout combination.
This turns things around: a controller is not bound to a specific View/Layout.

Let's make an example of the flow with the addUser functionality you mentioned as an example; I'll be referring to these files (but you'll have plenty more):

/controllers/user.php
/models/user.php
/views/useradd/view.html.php
/views/useradd/tmpl/default.php
/views/useradd/tmpl/default.xml
/controller.php
/router.php

As you can see the layouts are inside each view's tmpl folder.

router.php

Let's start from this last file: router.php defines our custom SEF rules so, after Joomla passes the call to our component (usually with the params ?option=com_componentname) we can takeover and interpret the URL as we wish. It is a bit hard to get started with but does provide the most flexibility and power. We don't really need to implement it at all for this simple example: so back to our registration now.

First step: show the "new user" form.

You would typically bind this to a menu item, pointing to the /views/useradd/tmpl/default.php; the /views/useradd/tmpl/default.xml contains the definition of the layout so it's available in the menu manager. Very often there is only one layout per view.

Control is passed to the view /views/useradd/view.html.php , and the view will then load an instance of its own model (automatically chosen based on the view name, you can load other models of course) to gather any initialization data.

The view then renders the layout, and presents it to the user. The layout's responsibility includes generating a form with an appropriate action (endpoint) and security tokens if appropriate:

<form action="index.php?option=com_mycomponent">
<input type="hidden" task="user.save">
<?php echo JHtml::_('form.token');?>

as you see it doesn't really matter if you want to use <input or params on the url, and you can most often mix them.

Form interaction

For autocompletion the form may need to invoke some backend controller methods, i.e. the method emailAvailable() in the /controllers/user.php

It does not make sense to have such functionality indexed, so we'll invoke the method directly with a non-SEF url:

index.php?option=com_ourcomponent&task=user.emailAvailable 

followed by any other parameter. This will work in both get and post.

The controller /controllers/user.php's emailAvailable() method will return a json structure and then invoke exit() as we don't want the CMS to kick in at all. An alternative solution is to add the param &format=json in the call.

{"email":"johndoe@example.com", "available":true}

Saving the data

When the user submits the form, processing is first handled by the controller since a task is specified. (see above task=user.save). Joomla will invoke the method save() in the controller /controllers/user.php.

This time, however, our controller is responsible for returning information to the user. After processing the data, it may choose to re-render the registration form showing an error, or a thank you page. In either case the controller simply sets the redirect, letting Joomla handle the rendering when appropriate.

$this->setRedirect(JRoute::_('index.php?option=com_yourcomponent&view=useradd', false));

More control

Each time a controller task is not specified, the display() method of the main controller is invoked. You can add custom logic there.

Joomla fires several events during a view rendering; these can be intercepted by a system plugin or - if you add in the calls - other kinds of plugins as well. You may even create your own types of plugins. Do not try to instantiate a view manually from a controller, as this may inhibit plugin firing.

Riccardo Zorn
  • 5,590
  • 1
  • 20
  • 36
1

Small insight,

1) Directory Structure

  controllers/
        controller1
        controller2
        controller3
    models/
        modelForTableA
        modelForTableB
    views/
       layout1

2) View and layout and task

check this answer

3) More routing techniques with SEF.

Hope it helps.

Community
  • 1
  • 1
Jobin
  • 8,238
  • 1
  • 33
  • 52
  • It seems that, referring to how url is writtern: option=COMPONENT_NAME&view=CONTROLLER_NAME&layout=IS_MY_ACTION? It's correct? – user2548436 Jan 05 '17 at 12:35
  • However usefull but it did not answer my question – user2548436 Jan 05 '17 at 13:36
  • No like this `index.php?option=com_users&task=registration.register` here is task is `registartion.register` means controller name `registration` and method name `register`. layout is views sub items – Jobin Jan 06 '17 at 04:11
0

solved with this. I cannot delete this question because there already exists other answer. Could any moderator close or delete this? Thank you

https://joomla.stackexchange.com/questions/18774/joomla-terminology-view-layout-task-and-component-development/18799#18799

Community
  • 1
  • 1
user2548436
  • 915
  • 2
  • 16
  • 35