Básico Absoluto de Como Funciona um Componente

From Joomla! Documentation

Revision as of 20:05, 12 December 2023 by FuzzyBot (talk | contribs) (Updating to match new version of source page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Other languages:
Bahasa Indonesia • ‎Deutsch • ‎English • ‎Nederlands • ‎Nederlands (informeel)‎ • ‎eesti • ‎español • ‎français • ‎italiano • ‎polski • ‎português • ‎português do Brasil • ‎română • ‎русский • ‎українська • ‎فارسی • ‎हिन्दी • ‎অসমীয়া • ‎বাংলা • ‎中文(台灣)‎

Este artigo foi criado para os principiantes no Joomlaǃ; Ele foi criado para explicar o que é um componente do Joomlaǃ e como este funciona. Quando um exemplo de componente específico beneficiará o tutorial, este artigo fará referência a um componente de exemplo chamado Olá, Mundo.

O que é um Componente Joomlaǃ

A component is a kind of Joomla! extension. Components are the main functional units of Joomla!; they can be seen as mini-applications. An easy analogy would be that Joomla! is the operating system and the components are desktop applications. Created by a component, content is usually displayed in the center of the main content area of a template (depending on the template).

Most components have two main parts: an administrator part and a site part. The site part is what is used to render pages of your site when they are requested by your site visitors during normal site operation. The administrator part provides an interface to configure and manage different aspects of the component and is accessible through the Joomla! administrator application.

Joomla! comes with a number of core components, like the content management system, contact forms and Web Links.

See also: Module, Plugin, Template


Na estrutura do Joomla!, os componentes podem ser criados utilizando um modelo simples (devolve código HTML para a página solicitada) ou o padrão "Modelo-Ver-Controlador" (aqui referido como MVC).

Introdução ao MVC

MVC é um padrão de desenho de software que pode ser utilizado para organizar código de tal forma que uma lógica de negócios e a apresentação de dados estão separados. A premissa por trás desta abordagem é que, se a lógica de negócios estiver agrupada numa secção, a interface e a interação do utilizador que rodeiam os dados poderão ser revistas e personalizadas sem a necessidade de reprogramar a lógica de negócios. MVC foi desenvolvido originalmente para mapear as funções tradicionais de entrada, processamento e saída numa arquitetura lógica da GUI.

Modelo

The model is the part of the component that encapsulates the application's data. It will often provide routines to manage and manipulate this data in a meaningful way in addition to routines that retrieve the data from the model. In general, the underlying data access technique should be encapsulated in the model. In this way, if an application is to be moved from a system that utilizes a flat file to store its information to a system that uses a database, the model is the only element that needs to be changed, not the view or the controller.

Visualização

The view is the part of the component that is used to render the data from the model in a manner that is suitable for interaction. For a web-based application, the view would generally be an HTML page that is returned to the user. The view pulls data from the model (which is passed to it from the controller) and feeds the data into a template which is populated and presented to the user. The view does not cause the data to be modified in any way, it only displays the data received from the model.

Controlador

The controller is responsible for responding to user actions. In the case of a web application, a user action is generally a page request. The controller will determine what request is being made by the user and respond appropriately by triggering the model to manipulate the data appropriately and passing the model into the view. The controller does not display the data in the model, it only triggers methods in the model which modify the data, and then pass the model into the view which displays the data.

Joomla! - Estrutura de Componente Explicada

Modelo

In the Joomla framework, models are responsible for managing the data. The first function that has to be written for a model is a get function. It returns data to the caller. For this example, the caller will be the HelloWorldViewHelloWorld view. By default, the model named HelloWorldModelHelloWorld residing in site/models/helloworld.php is the main model associated to this view. So let's have a quick look at the naming conventions with an example, since the naming convention are the actual magic that make everything work: The class HelloWorldViewHelloWorld resides in site/views/helloworld/view.html.php and will make use of the class HelloWorldModelHelloWorld in the file site/models/helloworld.php Let's just assume we want to use an imaginary view fluffy, you would have to have: The class HelloWorldViewFluffy which resides in site/views/fluffy/view.html.php. The view will make use of HelloWorldModelFluffy in the file site/models/fluffy.php. Note: the actual screen of the view: site/views/fluffy/tmpl/default.php is required as well to make this example work. Breaking any of these bold conventions will lead to errors or a blank page.

Aceder a um Componente Joomlaǃ

First we need to access the Joomla! platform, which is always accessed through a single point of entry. Using your preferred web browser, navigate to the following URL:

1 acesso de utilizador

<oseusite>/joomla/index.php

2 acesso de administrador

<oseusite>/joomla/administrator/index.php

Exemplo, Olá Mundo!: localhost/joomla/index.php You can use the URL of the component, or a Menu in order to navigate to the component. In this article we will discuss using the URL.

1 acesso de utilizador

<oseusite>/joomla/index.php?option=com_<nome_do_componente>

2 acesso de administrador

<oseusite>/joomla/administrator/index.php?option=com_<nome_do_componente>

Exemplo, Olá Mundo!: localhost/joomla/index.php?option=com_helloworld

Estrutura Básica da Diretoria MVC

Components are stored in a directory within your Joomla! installation, specifically at:

  • htdocs/<path_to_joomla>/components/com_<component_name>/ .

The Hello World! component would be stored in htdocs/<path_to_joomla>/components/com_helloworld/. A basic component will contain the following files within its directoryː

  • An HTML file that is just a security file with a background colorː index.html
  • A PHP file that represents the controller itselfː controller.php
  • A PHP file that loads the controller classː <component_name>.php
  • A PHP file that represents the model itselfː models/<component_name>.php
  • Another HTML file for background controlː models/index.html
  • A PHP file containing the default viewː views/<component_name>/tmpl/default.php
  • An XML file for adding a menu item typeː views/<component_name>/tmpl/default.xml
  • Another HTML file for background controlː views/<component_name>/tmpl/index.html
  • Another HTML file for background controlː views/<component_name>/index.html
  • A PHP file for displaying the viewː views/<component_name>/view.html.php

JEXEC

A linha seguinte é normalmente encontrada no início dos ficheiros PHP no Joomla!:

<?php
    defined('_JEXEC') or die('Restricted Access');

Isso permite um ponto de entrada seguro na plataforma do Joomla!. JEXEC contém uma explicação detalhada.

Tutoriais sobre Desenhar um Componente MVC

Para aprender a criar o seu próprio componente MVC, por favor, complete o tutorial para a sua versão do Joomla!.