How the Joomla MVC will call the functions in a custom component...?

2,933 views
Skip to first unread message

fornandakishore

unread,
Jul 27, 2012, 11:59:20 PM7/27/12
to joomla-de...@googlegroups.com
Hello to all,
How the Joomla MVC will call the functions in a custom component...?

I am creating some custom components in Joomla but seems I am missing something on the code.

Here are my doubts..

1. When I create a menu link the menu appears ex: www.site.com/index.php?option=com_mycom&view=default&layout=mylayout

=> I need to know how this will execute. It seems to me first it is calling view.html.php => display() function, here when it will go to controller task functions.

=> if the menu link is like the above one then my controller functions are not executed. Here how to call controller task functions from view.html.php

2. I am creating links in custom components like this : www.site.com/index.php?option=com_mycom&task=mytask

=> When we click on this link, first it is going to controller mytask function and from that task to view.html.php function and their it is calling specified view.

Here I am getting confusion, how the MVC work flow will be when the links are created by menus ?

Sample code :

[code]

My controller code is like this :

class mycomMycontroller extends JController {

function display () {
// some code
}

function mytask () {
$view = &$this -> getView('MyView', 'html');
$view -> setLayout('bookings');
$view -> Bookings();
}

}

My View.html.php file is like this :

function display () {
// some code
}

function Bookings ($tpl = null) {

$model =& JModel::getInstance('MyView', 'MyComModel');
$alert =& $model->fetchData();
$this -> assignRef('alert', $alert);
parent::display($tpl);
}

[/code]

suppose if I create a external menu link like : www.site.com/index.php?option=com_mycom&task=mytask then it is going smoothly to the mytask() function and it fetches the layout with values successfully..

But if I create a menu using menu manager then that link is like this : www.site.com/index.php?option=com_mycom&view=default&layout=bookings
And in this situation it is going to the display () function in view.html.php only...and not to controller mytask() or not to view.html bookings()

---------------------

Nanda Kishore. M

Senior PHP Developer

http://php-desk.blogspot.com

Mobile: + 91 98499 71144 


Niels Braczek

unread,
Jul 28, 2012, 6:46:08 AM7/28/12
to joomla-de...@googlegroups.com
Am 28.07.2012 05:59, schrieb fornandakishore:

> 1. When I create a menu link the menu appears ex: www.site.com/
> index.php?option=com_mycom&view=default&layout=mylayout
>
> => I need to know how this will execute. It seems to me first it is calling
> view.html.php => display() function, here when it will go to controller
> task functions.

http://docs.joomla.org/Component_Program_Flow is for Joomla 1.5 but the
big picture didn't change.

Regards,
Niels

--
| http://barcamp-wk.de · 2. Barcamp Westküste Frühjahr 2013 |
| http://www.bsds.de · BSDS Braczek Software- und DatenSysteme |
| Webdesign · Webhosting · e-Commerce · Joomla! Content Management |
------------------------------------------------------------------

Nick Weavers

unread,
Jul 29, 2012, 5:12:15 AM7/29/12
to joomla-de...@googlegroups.com
I posted a similar question some time ago:  https://groups.google.com/forum/?fromgroups#!search/nick$20weavers$20menu/joomla-dev-general/HLg10RUnB0A/M1pEx7Pyc4AJ

If you find a way to do it I would be interested to know.

Nick

Swapnil Shah

unread,
Jul 29, 2012, 10:00:19 AM7/29/12
to joomla-de...@googlegroups.com
I think there is distinction that need to be made. Nick hopefully this will answer you question too. 

When a user clicks on a menu item what are they doing? They are performing an action. By default the user is performing the display action. They don't want todo anything else yet but see the page. 

If you want to display different forms, why not have a drops down with a list of forms you want to display. Then in your model you can determine which form the user wants to see and you display that form. You can have different views for each of the forms if you choose. 

The controller looks for the task variable, since that is what you are retrieving from get/post and passing it to execute. If you use a dot separate task, for example: subcontroller.task, joomla will look for a sub controller and  a function with the name of "task". 

You can look at the login module. It performs two different actions. user.login and user.logout. Under the com_users/controller folder there is a file called user with a sub controller named UserControllerUser with functions called login and logout. 

You can have a menu item perform an action, you just need to have a request variable or variables that can be selected. You can specify that in An XML file in the tmpl folder inside the views/<viewname> folder. 

Hope that helps! 
Regards, 


Neil
Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To post to this group, send an email to joomla-de...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.

srgg01

unread,
Jul 29, 2012, 2:02:10 PM7/29/12
to joomla-de...@googlegroups.com

Yes, you'll get the task executed by controller directly only if you pass URL parameter 'task'. It will trigger an appropriate task in a controller. You see controllers mostly are for such a purpose.
However there are some tricks to do that in other way. You may catch any URL parameter in view.html.php (your default view, in display()).
After that you may do all you need - get any model, call any method etc.

for example:

   if (JRequest::getVar('myurl')) {
      $this->getModel('modelName')->myTaskMethod();
  }
(usualy, tasks implement in models).
But question is - what you  want to implement the task for without passing its URL parameter?
суббота, 28 июля 2012 г., 7:59:20 UTC+4 пользователь Nandu написал:

fornandakishore

unread,
Jul 29, 2012, 3:20:26 PM7/29/12
to joomla-de...@googlegroups.com
Thanks to all,

@srgg01

suppose if you have many layouts, some layout needs to fetch some data to display the layout information.

Here my question is you have created some menu items for different layouts, when you click on these menus by default all these will go the display() function in the view.html.php.

So if you want to fetch data for some particular layouts (not for all the layouts), how to call those particular functions for those layouts from the display() function.

And also is there any possibilities of calling a controller task from view.html.php
 
Thanks & Regards

Nanda Kishore. M

Senior PHP Developer

http://php-desk.blogspot.com

Mobile: + 91 98499 71144 




--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/Do36n7b42F8J.

srgg01

unread,
Jul 29, 2012, 3:52:09 PM7/29/12
to joomla-de...@googlegroups.com
>suppose if you have many layouts, some layout needs to fetch some data to display the layout information.
Here my question is you have created some menu items for different layouts, when you click on these menus by default all these will go the display() function in the view.html.php.

Sorry, I'm not quite uderstand - when you click on these menus by default all these will go the display() function in the view.html.php - do you mean that you WANT them to do that or you suppose that there it happens now?


суббота, 28 июля 2012 г., 7:59:20 UTC+4 пользователь Nandu написал:
Hello to all,

fornandakishore

unread,
Jul 30, 2012, 7:16:59 AM7/30/12
to joomla-de...@googlegroups.com
@srgg01,

I will try to explain bit clear here... when you create a menu items these links looks like the below...

index.php?option=com_mycom&view=myview&layout=testlayout1
index.php?option=com_mycom&view=myview&layout=testlayout2
index.php?option=com_mycom&view=myview&layout=testlayout3

In all these above cases by default when you click on these menu links it will take you to the display() function in the view.html.php

For suppose if you want to display some data for testlayout2 and testlayout3, so how can we do that only for those two layouts and not for all layouts.

Sorry for my bad english, I hope you can understand now.
 
Thanks & Regards

Nanda Kishore. M

Senior PHP Developer

http://php-desk.blogspot.com

Mobile: + 91 98499 71144 




--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/joomla-dev-general/-/OXqH4PM6NawJ.

Niels Braczek

unread,
Jul 30, 2012, 7:16:51 AM7/30/12
to joomla-de...@googlegroups.com
Am 30.07.2012 13:16, schrieb fornandakishore:

> index.php?option=com_mycom&view=myview&layout=testlayout1
> index.php?option=com_mycom&view=myview&layout=testlayout2
> index.php?option=com_mycom&view=myview&layout=testlayout3
>
> In all these above cases by default when you click on these menu links it
> will take you to the display() function in the view.html.php
>
> For suppose if you want to display some data for testlayout2 and
> testlayout3, so how can we do that only for those two layouts and not for
> all layouts.

In the controller (MycomController or MycomControllerMyview) display()
method, add all relevant models to the view. The layout files can then
draw all the data they want and need.

fornandakishore

unread,
Jul 30, 2012, 11:48:37 AM7/30/12
to joomla-de...@googlegroups.com
@Niels,

here my question how can we do that, Through display() function in view.html.php like the below or any other way.

I mean :  my view.html.php display() function

public function display() {

     $values = JRequest::get('request');
     if (!empty ($values['layout']) && $values['layout'] == testlayout2)) {
          // here my model calls
     }
}

Is this is the only way or any other ways ?
 
Thanks & Regards

Nanda Kishore. M

Senior PHP Developer

http://php-desk.blogspot.com

Mobile: + 91 98499 71144 




--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.

Niels Braczek

unread,
Jul 30, 2012, 11:15:58 AM7/30/12
to joomla-de...@googlegroups.com
Am 30.07.2012 17:48, schrieb fornandakishore:

> here my question how can we do that, Through display() function in
> view.html.php like the below or any other way.
>
> public function display() {
>
> $values = JRequest::get('request');
> if (!empty ($values['layout']) && $values['layout'] == testlayout2)) {
> // here my model calls
> }
> }

It doesn't harm to provide the models to all layouts.

In the controller:
public function display()
{
...
$view = $this->getView();
$view->setModel($mainModel, true);
$view->setModel($otherModel);
...
}

In tmpl/testlayout2.php, you can retrieve the models by name to get and
process additional data.

srgg01

unread,
Jul 30, 2012, 2:09:46 PM7/30/12
to joomla-de...@googlegroups.com
It seems to me my English is worser than yours one :)
Look, there is one standard way to manage all content. Beyond it using MVC just doesn't make sense.
When you load ANY page you ALWAYS first of all get a Component_name.php file which creates an instance of Controller.

You can find out there (I guess at any component_name.php file) such a code:

// Include dependancies
jimport('joomla.application.component.controller');
$controller    = JController::getInstance('Component_name');

Got it?

So if you think you escape a controller and get view.html.php passing it it's not true. Maybe it looks like controller does nothing but it doesn't mean that there it is not.
Do you guess that it means? It give you a possibility to manage all your content and scripts as you want.
If you dont't know how to load needed layout here is some ideas:
You can parse URL and then assign an actual layout to the view. Like this:

      function display()

      {

         // Set the view and the model

         $view   = JRequest::getVar( 'view' , 'current_view'  );

         $layout = JRequest::getVar( 'layout', 'default'  );

         $view  =& $this->getView( $view, 'html' );

         $model =& $this->getModel( 'modelName' );

         $view->setModel( $model, true );

         $view->setLayout( $layout  );

         // Display the content

         $view->display();

       }

After that it shold load that layout that was assigned.
Try it pls and tell us if you got success!

srgg01

unread,
Jul 30, 2012, 2:29:26 PM7/30/12
to joomla-de...@googlegroups.com
Of course, that code implements in a controller. And you may not to call a Model if you don't need that - there was just a standart approach to manage things.


On Saturday, 28 July 2012 07:59:20 UTC+4, Nandu wrote:
Reply all
Reply to author
Forward
0 new messages