Desarrollando un componente MVC : Añadiendo una variable request en el tipo de menu
Añadiendo una variable request en el tipo de menú
Por el momento, el mensaje mostrado es siempre Hello World!. Joomla!2.5 nos ofrece la posibilidad de añadir parámetros a los tipos de menú. En nuestro caso, esto lo hacemos en el archivo site/views/helloworld/tmpl/default.xml:
site/views/helloworld/tmpl/default.xml
COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC
Dos cosas importantes a tener en cuenta:
- El grupo de campos request indica campos obligatorios
- el atributo
namepuede pasarse al componente en la URL. En este caso ?option=com_helloworld&id=1 indicaría que has elegido la opción 1.
El modelo tiene que ser modificado para que cambie entre los dos diferentes mensajes (que será elegido por el usuario con el campo definido más arriba):
site/models/helloworld.php
msg))
{
//Uses JInput if magic quotes is turned off. Falls back to use JRequest.
if(!get_magic_quotes_gpc()) {
$id = JFactory::getApplication()->input->get('id', 1, 'INT' );
} else {
$id = JRequest::getInt('id');
}
switch ($id)
{
case 2:
$this->msg = 'Good bye World!';
break;
default:
case 1:
$this->msg = 'Hello World!';
break;
}
}
return $this->msg;
}
}
También modifica tu fichero helloworld.xml para indicar la nueva versión:
helloworld.xml
Hello World!
November 2009
John Doe
john.doe@example.org
http://www.example.org
Copyright Info
License Info
0.0.5
Description of the Hello World component ...
sql/updates/mysql
index.html
helloworld.php
controller.php
views
models
index.html
helloworld.php
sql
Puedes probar esta variable request poniendo index.php?option=com_helloworld&id=1 o index.php?option=com_helloworld&id=2 en el campo de direcciones de tu navegador.
Empaquetando el componente
Contenido del directorio:
- 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/sql/index.html
- admin/sql/updates/index.html
- admin/sql/updates/mysql/index.html
- admin/sql/updates/mysql/0.0.1.sql
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.