Implementing Business Logic

Components in Joomla 1.5

Purpose

The purpose of this presentation is to:
➔ ➔

Introduce the concept of business logic, Explain how business logic is represented in Joomla 1.5; Show how to implement business logic at various points of Joomla's MVC framework using the sample hello component.

Presentation name change me in master

What is Business Logic

Business rules that express business policy (such as channels, location, logistics, prices, and products); and Workflows that are the ordered tasks of passing documents or data from one participant (a person or a software system) to another. Business Logic is not about responding to a user's request ( Control Logic ) or presenting data to the user ( Display Logic ).

Presentation name change me in master

Examples of Business Logic

• • • • • • •

Login a user Placing an order Fetching information about a person Checking access controls Changing the state of a record Send a notification many more ...

Presentation name change me in master

Implementing Logic in Joomla

Joomla 1.0 • Call Function • Query Database • Show Data
( Procedural Code )

Joomla 1.5 • Implement MVC • Call Method • Display Data
( Object Oriented Code )

Presentation name change me in master

Joomla 1.5's MVC

• • • •

Model-View-Controller Software Design Pattern Business, presentation, and control logic are separate You can change one part without affecting another ( yay ! ) Joomla 1.5 is the first version of Joomla that uses a MVC design pattern ( double yay ! )
Presentation name change me in master

What is an MVC

Model - The model is the part of the component that encapsulates the application’s data and business logic. View - The view is the part of the component that is used to render the data from the model. Controller - 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.
Presentation name change me in master

Just Remember

Just remember: • Business logic is about rules and workflows. • The MVC is a design pattern, and the model can represent business logic. • Joomla 1.5 components are based on an MVC.

Presentation name change me in master

New to the MVC?

The Joomla Dev Website offers a sample hello component that you can experiment with.

Presentation name change me in master

File System Layout

Files that you will see in the hello tutorial once installed.
hello.php models/hello.php view.html.php default.php Controller Model View Layout File

http://dev.joomla.org/component/option,com_jd-wiki/Itemid,/id,tutorials:components/
Presentation name change me in master

Typical Flow of Execution

Controller View Method Layout

Determine what the user wants to do. Prepare the response.

Perform the business logic.

Display the results.

Presentation name change me in master

What Does a Model Look Like

➔ ➔

Class that extends Joomla's JModel Your method representing business logic
Presentation name change me in master

View from the Frontend

index.php?option=com_hello
Presentation name change me in master

Modification to the View

Add code to fetch a specific greeting.
Presentation name change me in master

View from the Frontend 2

Our presentation logic has not changed.

index.php?option=com_hello&id=2
Presentation name change me in master

Quick Review

So far we've said that: • Models represent business logic; • Models do not represent presentation or control logic. • Joomla's Jmodel class can be used in a component to represent your business logic.

Presentation name change me in master

Displaying Data

We still need to display the data. That is where the views and the layout files come in. Generally speaking, models are accessed from the views indirectly as show below.

Presentation name change me in master

Accessing the Business Logic

The method getGreeting() is called from the view class.
Presentation name change me in master

Modifying the Presentation Logic

The view contains the logic for displaying the data.
Presentation name change me in master

Layout files are used to display the data. Your template files should be the few that contain HTML.
default.php

nogreeting.php

Presentation name change me in master

Quick Review

So far we've learned that: • Models represent business logic; • Views represent presentation logic; • Layouts are for markup language.

Presentation name change me in master

Controller tasks

You may not need to customize your controller because Joomla provides most of the control logic for you. However you can define your own methods if you want. You may want to throw an error if an id has not been provided.

Presentation name change me in master

Custom Controller Method
Presentation name change me in master

Using Controller Methods

index.php?option=com_hello&task=showGreeting
Presentation name change me in master

Conclusion

The important concepts to take away are: • Controllers represent control logic; • Models represent business logic; • Views represent presentation logic; • Layouts are for markup language. Joomla 1.5 provides new facilities for you to easily represent your business logic.

Presentation name change me in master