Joomla Terminology: view, layout, task and component development

53digitalpro

New member
XNullUser
Joined
Apr 6, 2024
Messages
2
Reaction score
0
Points
1
Location
Victoria
NullCash
27
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.
 

pinter

New member
XNullUser
Joined
Apr 27, 2022
Messages
16
Reaction score
1
Points
3
NullCash
9
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.
This is an excellent post. Conscise and accurate. Thank you for taking the time to put this together!
 
Top