Actions

Difference between revisions of "Absolute Basics of How a Component Functions"

From Joomla! Documentation

m
Line 55: Line 55:
 
<source lang="php">
 
<source lang="php">
 
class <name>Controller extends JController
 
class <name>Controller extends JController
 +
 
{
 
{
function display()
+
  function display()
{
+
  {
echo 'displaying';
+
    echo 'displaying';
}
+
  }
 
}
 
}
 +
 
</source>
 
</source>
  
Line 66: Line 68:
  
 
<source lang="php">
 
<source lang="php">
 +
 
class <name>Controller extends JController
 
class <name>Controller extends JController
 
{
 
{
function jump()
+
  function jump()
{
+
  {
echo 'jumping';
+
    echo 'jumping';
}
+
  }
 
}
 
}
 +
 
</source>
 
</source>
  
Line 81: Line 85:
 
There is nothing that forces you to use the 'task' variable to drive the call because you can pass the value of any variable as the parameter to the execute method call. But in order to stick to the non-written rules, 'task' is usually used (and as it is treated specially by the system, it is a good idea to stick with it).
 
There is nothing that forces you to use the 'task' variable to drive the call because you can pass the value of any variable as the parameter to the execute method call. But in order to stick to the non-written rules, 'task' is usually used (and as it is treated specially by the system, it is a good idea to stick with it).
  
To trigger the views, you have to call the display() method of JController. Do this by inserting, in your method, a call to parent::display() as in the last line. At a minimum, your controller file should contain the following: (?Is this necessary? The display function should be pulled from the JController class that it is extending, right?)
+
To trigger the views, you have to call the display() method of JController. Do this by inserting, in your method, a call to parent::display() as in the last line. At a minimum, your controller file should contain the following:  
  
 
<source lang="php">
 
<source lang="php">
 +
 
jimport('joomla.application.component.controller');
 
jimport('joomla.application.component.controller');
  
 
class <name>Controller extends JController
 
class <name>Controller extends JController
 
{
 
{
function display()
+
  function display()
{
+
  {
parent::display();
+
    parent::display();
}
+
  }
 
}
 
}
 +
 
</source>
 
</source>
  
Line 112: Line 118:
 
When you built your request, you included a variable named 'view' that tells the MVC model what view you want. Or, if you did not include it, you need to include it now because there is no such a thing as a default view. So your URL is something like:
 
When you built your request, you included a variable named 'view' that tells the MVC model what view you want. Or, if you did not include it, you need to include it now because there is no such a thing as a default view. So your URL is something like:
  
http://example.com/index.php?option=com_<name>&view=<myview>[&task=<mytask>]
+
 
 +
<code><nowiki>http://example.com/index.php?option=com_<name>&view=<myview>[&task=<mytask>]</nowiki></code>
 +
 
  
 
The task part may or may not exist. Remember that if you omit it you are defaulting to task=display. With this URL Joomla! is importing a file located at <site root dir>/components/<name>/views/<myview>/view.html.php. If this files or the path does not exist, Joomla! will fail. By simply swapping the value of the view, you deliver different views of your data.
 
The task part may or may not exist. Remember that if you omit it you are defaulting to task=display. With this URL Joomla! is importing a file located at <site root dir>/components/<name>/views/<myview>/view.html.php. If this files or the path does not exist, Joomla! will fail. By simply swapping the value of the view, you deliver different views of your data.
Line 118: Line 126:
 
Every request for a view requires that you also specify the format you are serving the view. There exist several well known formats such as html (the default one if none is specified), rss, etc. but you can use your own. If no format is specified in the request with the 'format=<myformat>' parameter a default value of 'html' is used.
 
Every request for a view requires that you also specify the format you are serving the view. There exist several well known formats such as html (the default one if none is specified), rss, etc. but you can use your own. If no format is specified in the request with the 'format=<myformat>' parameter a default value of 'html' is used.
  
The 'html' format makes the Joomla! framework wrap the response in whatever template your site is using so that you get a fully built HTML page. This way, with very few effort from you side, you get back your page fully loaded with modules or whatever you had configured.
+
The 'html' format makes the Joomla! framework wrap the response in whatever template your site is using so that you get a fully built HTML page. This way, with very little effort from you, you get back your page fully loaded with modules or whatever you had configured.
  
 
The specific format you are using is what you have written in the middle part of the name of the file in your view folder (The file we talked about a few lines before 'view.html.php'). If you use a different format like 'rss' your file should be named after it like view.rss.php. Get it?
 
The specific format you are using is what you have written in the middle part of the name of the file in your view folder (The file we talked about a few lines before 'view.html.php'). If you use a different format like 'rss' your file should be named after it like view.rss.php. Get it?
  
As told before, you can have formats other than html and Joomla! will not wrap the template on them. You could have a 'pdf' format to deliver your data in pdf format or even an 'ajax' format to deliver ajax responses to the front-end easily. Just construct your URL like
+
As mentioned before, you can have formats other than html and Joomla! will not wrap the template on them. You could have a 'pdf' format to deliver your data in pdf format or even an 'ajax' format to deliver ajax responses to the front-end easily. Just construct your URL like
 +
 
 +
 
 +
<code><nowiki>http://example.com/index.php?option=com_<name>&view=<myview>&format=ajax</nowiki></code>
  
http://example.com/index.php?option=com_<name>&view=<myview>&format=ajax
 
  
 
to make the Joomla! Framework look for and load the file view.ajax.php located at <site root dir>/components/<name>/views/<myview>/ from where you can echo anything you want. It's that easy.
 
to make the Joomla! Framework look for and load the file view.ajax.php located at <site root dir>/components/<name>/views/<myview>/ from where you can echo anything you want. It's that easy.
  
Anyway, to achieve your goal you need some code inside the view.<format>.php file. You have the view file, now you need the view class. You have to extend the JView class with your own class following the strict rules, mentioned before, that you should follow. In this case, your class name must be built by concatenating the component name, the word 'View', and the view name. So your class name will be a capitalized <name>View<myview>. If your component is named travels and your view is named detail (URL ...?option=com_read&view=detail) your view class must be:
+
Anyway, to achieve your goal, you will need some code inside the view.<format>.php file. You have the view file, now you need the view class. You have to extend the JView class with your own class following the strict rules mentioned before. In this case, your class name must be built by concatenating the component name, the word 'View', and the view name. So your class name will be a capitalized <name>View<myview>. If your component is named travels and your view is named detail (URL ...?option=com_travels&view=detail) your view class must be:
  
 
<source lang="php">
 
<source lang="php">
 +
 
class TravelsViewDetail extends JView
 
class TravelsViewDetail extends JView
 
{
 
{
function display($tpl=null)
+
  function display($tpl=null)
{
+
  {
echo 'blah, blah';
+
    echo 'blah, blah';
}
+
  }
 
}
 
}
 +
 
</source>
 
</source>
  
Line 157: Line 169:
  
 
<source lang="php">
 
<source lang="php">
 +
 
class <Name>View<Viewname> extends JView
 
class <Name>View<Viewname> extends JView
 
{
 
{
function display($tpl=null)
+
  function display($tpl=null)
{
+
  {
// Prepare the data
+
$data1 = ....
+
$data2 = ....
+
$moredata[] = array....
+
  
// Inject the data
+
    // Prepare the data
$this->assignRef('variablename', $data1);
+
    $data1 = ....
$this->assignRef('variablename2', $data2);
+
    $data2 = ....
$this->assignRef('variablename3', $moredata);
+
    $moredata[] = array....
  
// Call the layout template
+
    // Inject the data
$tpl = 'myTemplate';
+
    $this->assignRef('variablename', $data1);
parent::display($tpl);
+
    $this->assignRef('variablename2', $data2);
 +
    $this->assignRef('variablename3', $moredata);
  
or more directly
+
    // Call the layout template
 +
    $tpl = 'myTemplate';
 +
    parent::display($tpl);
  
parent::display('myTemplate');
+
    or more directly
}
+
 
 +
    parent::display('myTemplate');
 +
  }
 
}
 
}
 
</source>
 
</source>
Line 188: Line 202:
 
If you have been observant you will have noticed that you have not 'used' the 'model' part of MVC model. Here you have the last point of decision. You can go without this part or apply fully the model but I think I will keep this tale for another session. For sure I have already abused of my audience.
 
If you have been observant you will have noticed that you have not 'used' the 'model' part of MVC model. Here you have the last point of decision. You can go without this part or apply fully the model but I think I will keep this tale for another session. For sure I have already abused of my audience.
  
[[Category:Tips and tricks]]
+
[[Category:Tutorials]]
[[Category:Tips and tricks 1.5]]
+
[[Category:Explanations]]
 
+
[[Category:Component Development]]
[[Category:Tutorials]][[Category:Component Development]]
+

Revision as of 21:42, 22 August 2012