Desarrollando un componente MVC : Añadiendo una variable request en el tipo de menu

  1. Desarrollando un componente MVC : Introducción
  2. Desarrollando un componente MVC: Desarrollando un componente básico
  3. Desarrollando un componente MVC: Añadiendo una vista en el frontend
  4. Desarrollando un componente MVC: Añadiendo un tipo de menu al frontend
  5. Desarrollando un componente MVC: Añadiendo un modelo al frontend
  6. Desarrollando un componente MVC : Añadiendo una variable request en el tipo de menu
  7. Desarrollando un componente MVC : Usando la base de datos
  8. Desarrollando un componente MVC : Basic backend
  9. Desarrollando un componente MVC : Añadiendo gestión de idioma
  10. Desarrollando un componente MVC : Añadiendo acciones backend
  11. Desarrollando un componente MVC : Añadiendo adornos al backend
  12. Desarrollando un componente MVC : Añadiendo verificaciones
  13. Desarrollando un componente MVC : Añadiendo categorías
  14. Desarrollando un componente MVC : Añadiendo configuración
  15. Desarrollando un componente MVC : Añadiendo ACL (Access Control Levels)
  16. Desarrollando un componente MVC : Añadiendo un script de instalación-desinstalación-actualización
  17. Desarrollando un componente MVC : Usando la capacidad de filtro
  18. Desarrollando un componente MVC : Añadiendo un servidor de actualización

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 name puede 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
        
 
        
                
                Hello World!
                
                
                
                        
                        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.

Deja un comentario