Developing a Model-View-Controller Component for Joomla

Slides:



Advertisements
Similar presentations
Apache Struts Technology
Advertisements

About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
ASP.NET Programming with C# and SQL Server First Edition
Struts 2.0 an Overview ( )
UNIT-V The MVC architecture and Struts Framework.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
A First Program Using C#
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Dynamic Action with Macromedia Dreamweaver MX Barry Sosinsky Valda Hilley.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Design Patterns Phil Smith 28 th November Design Patterns There are many ways to produce content via Servlets and JSPs Understanding the good, the.
LiveCycle Data Services Introduction Part 2. Part 2? This is the second in our series on LiveCycle Data Services. If you missed our first presentation,
Tutorial 121 Creating a New Web Forms Page You will find that creating Web Forms is similar to creating traditional Windows applications in Visual Basic.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
File Structures Foundations of Computer Science  Cengage Learning.
Storing and Retrieving Data
Model View Controller MVC Web Software Architecture.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
ASP. What is ASP? ASP stands for Active Server Pages ASP is a Microsoft Technology ASP is a program that runs inside IIS IIS stands for Internet Information.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
CSC 2720 Building Web Applications Basic Frameworks for Building Dynamic Web Sites / Web Applications.
Project 5: Using Pop-Up Windows Essentials for Design JavaScript Level One Michael Brooks.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Introduction  “M” “V” “C” stands for “MODEL” “VIEW” “CONTROLLER”. ASP.NET MVC is an architecture to develop ASP.NET web applications in a different manner.
1 Copyright © 2008, Oracle. All rights reserved. Repository Basics.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Chapter 1 Getting Started with ASP.NET Objectives Why ASP? To get familiar with our IDE (Integrated Development Environment ), Visual Studio. Understand.
Section 8.1 Section 8.2 Create a custom theme Design a color scheme
Section 10.1 Define scripting
Development Environment
Objective % Select and utilize tools to design and develop websites.
Lesson # 9 HP UCMDB 8.0 Essentials
Essentials of UrbanCode Deploy v6.1 QQ147
Working in the Forms Developer Environment
CONTENT MANAGEMENT SYSTEM CSIR-NISCAIR, New Delhi
Data Virtualization Tutorial… CORS and CIS
CONTENT MANAGEMENT SYSTEM CSIR-NISCAIR, New Delhi
Play Framework: Introduction
Module 4 Remote Login.
Introduction to Triggers
Section 17.1 Section 17.2 Add an audio file using HTML
MVC and other n-tier Architectures
Content Management System
AVOIR -African virtual
Core LIMS Training: Advanced Administration
Arrays and files BIS1523 – Lecture 15.
Haritha Dasari Josue Balandrano Coronel -
Objective % Select and utilize tools to design and develop websites.
Introduction to Events
Top Reasons to Choose Angular. Angular is well known for developing robust and adaptable Single Page Applications (SPA). The Application structure is.
MVC Framework, in general.
James Blankenship March , 2018
Model-View-Controller Patterns and Frameworks
Building Web Applications
Objective Understand web-based digital media production methods, software, and hardware. Course Weight : 10%
Developing a Model-View-Controller Component for Joomla Part 3
Developing a Model-View-Controller Component for Joomla Part 2
Classes and Objects.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Web Development Using ASP .NET
Using Templates and Library Items
Lecture 5: Functions and Parameters
Fundaments of Game Design
Wordpress test.cs.edinboro.edu.
An Introduction to JavaScript
CMSC 202 Exceptions.
Web Application Development Using PHP
Presentation transcript:

Developing a Model-View-Controller Component for Joomla

Introduction A software framework is the base of an application that can be used by a developer. The framework in Joomla! 1.5 unleashes a great deal of power for them. The Joomla! code has been designed for extensibility. This presentation will guide you through the process of developing a component using the framework. The scope of this project will be to develop a simple Hello World! component. In future tutorials, this simple framework will be built upon to show the full power and versatility of the MVC design pattern in Joomla!

Introduction to Model-View-Controller While the idea behind a component may seem extremely simple, code can quickly become very complex as additional features are added or the interface is customized. Model-View-Controller (herein referred to as MVC) is a software design pattern that can be used to organize code in such a way that the business logic and data presentation are separate. The premise behind this approach is that if the business logic is grouped into one section, then the interface and user interaction that surrounds the data can be revised and customized without having to reprogram the business logic. MVC was originally developed to map the traditional input, processing, output roles into a logical GUI architecture. These three main roles are the basis for the Joomla MVC. They are described here in brief, but for a more thorough explanation, please refer to the links provided at the end of this tutoria

Model 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 our case, the model will contain methods to add, remove and update information about the greetings in the database. It will also contain methods to retrieve the list of greetings from the database. 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.

View 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 data retrieved from the model.

Controller 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.

MVC connection The simplified picture on the right depicts the basic components being used within Joomla. Besides the Model, the View and the Controller, an Entry Point has been added that is depicted as a small circle. Attached to the viewer (view) a Template has been added. With these five components you should be able to understand this tutorial about making a basic Joomla! MVC component.

Joomla! MVC Implementation In Joomla!, the MVC pattern is implemented using three classes: JModel, JView and JController

Creating a Component For our basic component, we only require five files: site/hello.php - this is the entry point to our component site/controller.php - this file contains our base controller site/views/hello/view.html.php - this file retrieves the necessary data and pushes it into the template site/views/hello/tmpl/default.php - this is the template for our output hello.xml - this is an XML (manifest) file that tells Joomla! how to install our component. Remember that the filename for the entry point must have the same name of the component. For example, if you call your component "Very Intricate Name Component", at install time Joomla! will create the folder com_veryintricatenamecomponent and the entry point php file must be named veryintricatenamecomponent.php otherwise it will not work. Be aware that use of some special characters, notibly the underscore '_', may have special meaning in Joomla and should be avoided in component names or files. The site directory here is for the parts of the component which are installed in the front end site.

Naming conventions At this point it is important to say that some words are reserved for using in component or its class names, and violating some of that will produce a hard for debugging error. One of them is "view" (in any character case) for view class (subclass of JView) and controller class (subclass of JController), because view class need to have first part of name the same as controller class name, and component name (although violating of last one won't produce an error, it's just a useful convention). All filenames and foldernames for models, views and controllers must be lower-case in order to operate well on Unix/Linux-systems.

Creating the Entry Point Joomla! is always accessed through a single point of entry: index.php for the Site Application or administrator/index.php for the Administrator Application. The application will then load the required component, based on the value of 'option' in the URL or in the POST data. For our component, the URL would be: index.php?option=com_hello&view=hello This will load our main file, which can be seen as the single point of entry for our component: components/com_hello/hello.php.

site/hello.php

The first statement is a security check. JPATH_COMPONENT is the absolute path to the current component, in our case components/com_hello. If you specifically need either the Site component or the Administrator component, you can use JPATH_COMPONENT_SITE or JPATH_COMPONENT_ADMINISTRATOR. DS is the directory separator of your system: either '/' or '\'. This is automatically set by the framework so the developer doesn't have to worry about developing different versions for different server OSs. The 'DS' constant should always be used when referring to files on the local server. After loading the base controller, we check if a specific controller is needed. In this component, the base controller is the only controller, but we will leave this conditional check "in place" for future use. JRequest::getWord() finds a word variable in the URL or the POST data. So if our URL is index.php?option=com_hello&controller=controller_name, then we can retrieve our controller name in our component using: echo JRequest::getWord('controller'); Now we have our base controller 'HelloController' in com_hello/controller.php, and, if needed, additional controllers like 'HelloControllerController1' in com_hello/controllers/controller1.php. Using this standard naming scheme will make things easy later on: '{Componentname}{Controller}{Controllername}' After the controller is created, we instruct the controller to execute the task, as defined in the URL: index.php?option=com_hello&task=sometask. If no task is set, the default task 'display' will be assumed. When display is used, the 'view' variable will decide what will be displayed. Other common tasks are save, edit, new... The controller might decide to redirect the page, usually after a task like 'save' has been completed. This last statement takes care of the actual redirection. The main entry point (hello.php) essentially passes control to the controller, which handles performing the task that was specified in the request. Note that we don't use a closing php tag in this file: ?>. The reason for this is that we will not have any unwanted whitespace in the output code. This is default practice since Joomla! 1.5, and will be used for all php-only files.

Creating the Controller

The JController constructor will always register a display() task and unless otherwise specified (using the registerDefaultTask() method), it will set it as the default task. This barebones display() method isn't really even necessary since all it does is invoke the parent constructor. However, it is a good visual clue to indicate what is happening in the controller. The JController::display() method will determine the name of the view and layout from the request and load that view and set the layout. When you create a menu item for your component, the menu manager will allow the administrator to select the view that they would like the menu link to display and to specify the layout. A view usually refers to a view of a certain set of data (i.e. a list of cars, a list of events, a single car, a single event). A layout is a way that that view is organized. In our component, we will have a single view called hello, and a single layout (default).

Creating the View The task of the view is very simple: It retrieves the data to be displayed and pushes it into the template. Data is pushed into the template using the JView::assignRef method. (Note: The key (the first argument) passed to the assignRef method cannot be preceded by an underscore i.e. $this->assignRef('_greeting',$greeting). Doing so will cause the assignRef method to return false and your variable will not be pushed into the template.) The code for the view at site/views/hello/view.html.php: