Desarrollando un componente MVC : Basic backend
Basic backend
Designing the backend interface leads us to create at least a Model-View-Controller triptych. We have to modify the administrator entry point of our component, the admin/helloworld.php file
admin/helloworld.php
input;
$task = $jinput->get('task', "", 'STR' );
// Perform the Request task
$controller->execute($task);
// Redirect if set by the controller
$controller->redirect();
Create the general controller
The entry point now gets an instance of a HelloWorld prefixed controller. Let’s create a basic controller for the administrator part:
admin/controller.php
input;
$input->set('view', $input->getCmd('view', 'HelloWorlds'));
// call parent behavior
parent::display($cachable);
}
}
This controller will display the ‘HelloWorlds’ view by default.
Create the view
With your favorite file manager and editor, create a file admin/views/helloworlds/view.html.php containing:
admin/views/helloworlds/view.html.php
get('Items');
$pagination = $this->get('Pagination');
// Check for errors.
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('
', $errors));
return false;
}
// Assign data to the view
$this->items = $items;
$this->pagination = $pagination;
// Display the template
parent::display($tpl);
}
}
In Joomla, views display data using layout. With your favorite file manager and editor, put a file admin/views/helloworlds/tmpl/default.php containing
admin/views/helloworlds/tmpl/default.php
This layout calls several sub-layout (head, foot and body). Each sub-layout corresponds to a file prefixed by the name of the main layout (default), and an underscore.
Put a file admin/views/helloworlds/tmpl/default_head.php containing
admin/views/helloworlds/tmpl/default_head.php
COM_HELLOWORLD_HELLOWORLD_HEADING_ID and COM_HELLOWORLD_HELLOWORLD_HEADING_GREETING are placeholders which will later be replaced with language-specific text. The JText::_ method translates a string into the current language.
checkAll is a javascript function defined in the Joomla core able to check all items.
Put a file admin/views/helloworlds/tmpl/default_body.php containing
admin/views/helloworlds/tmpl/default_body.php
items as $i => $item): ?>
id; ?>
id); ?>
greeting; ?>
JHtml::_ is a helper function able to display several HTML output. In this case, it will display a checkbox for the item.
Put a file admin/views/helloworlds/tmpl/default_foot.php containing
admin/views/helloworlds/tmpl/default_foot.php
pagination->getListFooter(); ?>
JPagination is a Joomla class able to manage and display pagination object.
Create the model
The HelloWorlds view asks the model for data. In Joomla, there is a class able to manage a list of data: JModelList. Class JModelList and inherited classes need only one method:
getListQuery which constructs an SQL query and two states:
- list.start for determining the list offset
- list.limit for determining the list length
The getItems and getPagination methods are defined in JModelList class. They don’t need to be defined in the HelloWorldModelHelloWorlds class.
admin/models/helloworlds.php
getQuery(true);
// Select some fields
$query->select('id,greeting');
// From the hello table
$query->from('#__helloworld');
return $query;
}
}
The _populateState method is, by default, automatically called when a state is read by the getState method.
Packaging the component
Content of your code directory
helloworld.xml
site/index.html
site/helloworld.php
site/controller.php
site/views/index.html
site/views/helloworld/index.html
site/views/helloworld/view.html.php
site/views/helloworld/tmpl/index.html
site/views/helloworld/tmpl/default.xml
site/views/helloworld/tmpl/default.php
site/models/index.html
site/models/helloworld.php
admin/index.html
admin/helloworld.php
admin/controller.php
admin/sql/index.html
admin/sql/install.mysql.utf8.sql
admin/sql/uninstall.mysql.utf8.sql
admin/sql/updates/index.html
admin/sql/updates/mysql/index.html
admin/sql/updates/mysql/0.0.1.sql
admin/sql/updates/mysql/0.0.6.sql
admin/models/index.html
admin/models/fields/index.html
admin/models/fields/helloworld.php
admin/models/helloworlds.php
admin/views/index.html
admin/views/helloworlds/index.html
admin/views/helloworlds/view.html.php
admin/views/helloworlds/tmpl/index.html
admin/views/helloworlds/tmpl/default.php
admin/views/helloworlds/tmpl/default_head.php
admin/views/helloworlds/tmpl/default_body.php
admin/views/helloworlds/tmpl/default_foot.php
admin/tables/index.html
admin/tables/helloworld.php
Create a compressed file of this directory or directly download the archive and install it using the extension manager of Joomla. You can add a menu item of this component using the menu manager in the backend.
helloworld.xml
Hello World!
November 2009
John Doe
john.doe@example.org
http://www.example.org
Copyright Info
License Info
0.0.7
Description of the Hello World component ...
sql/install.mysql.utf8.sql
sql/uninstall.mysql.utf8.sql
sql/updates/mysql
index.html
helloworld.php
controller.php
views
models
index.html
helloworld.php
controller.php
sql
tables
models
views
Now you can see in your component hello-world an array with two colums, two rows and checkboxes. You can click the checkboxes in order to select the different options you want.