28
SourceCoast Extensions sourcecoast.com Intro To Joomla Development Joomla Development It’s Easier Than You Think 1 Saturday, August 6, 11

Intro to Joomla Development

Embed Size (px)

DESCRIPTION

This session was presented at Joomla Day Chicago as an introduction to Joomla Development in Joomla 1.6 and beyond. http://www.sourcecoast.com/

Citation preview

Page 1: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

Joomla Development

I t ’ s E a s i e r T h a n Y o u T h i n k

1Saturday, August 6, 11

Page 2: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

SourceCoastA l e x A n d r e a e

O w n e r / D e v e l o p e r

JFBConnectFull Facebook Integration Suite for Joomla

* Facebook Authentication* Facebook Page Tab / Canvas Support* Like, Comment, Fanbox, Recommendations & more widgets* Customizeable Profile Import Into

* JomSocial, Community Builder, Kunena, K2, Agora* Lots more...

Over 60 straight 5-star reviews on the JED

2Saturday, August 6, 11

Page 3: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

Overview

Module Structure

API Overview

Component Structure

3Saturday, August 6, 11

Page 4: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

Joomla - MVC

MVC(t) – Model View Controller (template)

Controller – Sets up environment for the display

Model – Data interface

Generally to the database, but can 'create' data if necessary

View – Uses the model to get data to display

Template – Override-able display output

4Saturday, August 6, 11

Page 5: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

ModulesLinear code execution

VC(t) (no real model necessary)

Uses a 'helper.php' file for additional function calls

Can be model-like, but modules don’t have DB tables

Files:

mod_<modulename>.php and .xml

helpers/helper.php

tmpl/default.php

5Saturday, August 6, 11

Page 6: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

Modules - Basics

mod_demo.php

default.php

mod_demo.xml

6Saturday, August 6, 11

Page 7: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

Module Helper Filesmod_demo.php

helper.php

default.php

* Separate out Classes/Functions

7Saturday, August 6, 11

Page 8: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

Modules/Component Interactionmod_demo.php

helper.php

default.php

* Load Component’s model* Re-Use Code!

8Saturday, August 6, 11

Page 9: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

Joomla API Overview

9Saturday, August 6, 11

Page 10: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

Joomla APIhttp://api.joomla.com

Recently updated to Joomla 1.7 API - YAY!

Left side, select “Joomla-Platform” package

Bookmark, cuddle up to, and learn to love this page

“Classes” describes

JFactory – Most widely used

JRoute – Get URL for specific component/view/task

“Application” describes

Every other Joomla function (many inherited into JFactory)

10Saturday, August 6, 11

Page 11: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

JObject

All Joomla classes derived from (You should too)

Easy getter/setter methods

$obj->set(‘var’, ‘value’); // Set a variable

$obj->get(‘var’, ‘default’); // Get a value

$obj->def(‘var’, ‘default’); // Set a default

$obj->toString(); // Great for debugging (no print_r($obj) required!

11Saturday, August 6, 11

Page 12: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

JUser Object$user = JFactory::getUser(); // logged in user

$user = JUser::getInstance($id);

$id = UID or username

Once fetched, use

if ($user->guest)

$user->get('username'); // id, fullname, lastvisitDate, etc

It’s derived from JObject!

12Saturday, August 6, 11

Page 13: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

JFactory - Database$dbo =& JFactory::getDBO() // Get the database handle

Functions described in JDatabase & JDatabaseMySQL(i)

$query = $dbo->getQuery(true); // New Query$query->select(“id, username, email”);$query->from(“#__users”);$query->where(“username=”.$dbo->quote($username); // No shenanigans!$dbo->setQuery($query);

$dbo->setQuery(“SELECT id,username,email FROM #__users WHERE username = “.$dbo->quote($username)); // Not as flexible

$user = $dbo->loadObject(); // objectList, array, arrayList, result, etc

Can use InsertObject function (or raw SQL) to add rows to database

13Saturday, August 6, 11

Page 14: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

URL RoutingJRoute

JRoute::_('index.php?option=com_content&view=article&id=12');

Will determine correct SEF URL, if SEF is enabled

Also, SEF components (sh404, Artio, etc) override this call, so it works for them too!

JURI

$uri = JFactory::getURI(); // Get current query string

$uri->setVar('view', 'blah'); // Add/change value

JRoute::_('index.php?'.$uri->getQuery()); // Create new route

14Saturday, August 6, 11

Page 15: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

JApplicationHandle to the Application class

$app =& JFactory::getApplication();

$app->enqueueMessage('blah', 'error');

$app->redirect($url); // Use JRoute!

$app->isAdmin()

$app->isSite()

Formerly global $mainframe. Globals are bad..

15Saturday, August 6, 11

Page 16: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

JTextText translation from language files

JText::_('COM_JFBCONNECT_LOGIN_MESSAGE’);

Searches loaded language files in last loaded order

Searches for first, exact, upper case version

If found, use translation; If not, use text directly

To include more language files:

$lang = JFactory::getLanguage(); // Get language object

$lang->load('com_jfbconnect'); // Add your file

16Saturday, August 6, 11

Page 17: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

JRequest

JRequest::getVar('name', 'default', 'POST');

Can be risky depending on use. Preferred to use the below 'sanitized' methods

JRequest::getInt('name', 4); // Integers only, default 4

JRequest::getCmd('task'); // strip \./

JRequest::getString('name'); // No nasty HTML

17Saturday, August 6, 11

Page 18: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

JFilterInput

Clean data before using in code

Used to prevent SQL, XSS, and other shenanigans or exploits

$filter =& JFilterInput::getInstance();

$filter->clean($var, ‘string’); //int, etc

Similar to (and used by) JRequest, but can be used on any data, not just environment variables

18Saturday, August 6, 11

Page 19: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

Jsession

Store information volatile information for user

Form information on validation failure

Information before a redirection (to login, etc)

$session = JSession::getInstance();

$session->set('city', $cityInput); // Set now

$city = $session->get('city', ''); // Get later

$session->clear('city');

19Saturday, August 6, 11

Page 20: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

Component Structure

20Saturday, August 6, 11

Page 21: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

Component Structure

True MVC(t) structure

<component_name>.php – entry-point. Determines controller and calls task on it

controller.php – Sets up the model(s) and other data structures to be used

/models/<model>.php – Data interface

/viewname/view.html.php – Uses the models to fetch and setup the data for the template

/viewname/tmpl/default.php (or any other template file)

21Saturday, August 6, 11

Page 22: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

Component Entry Point

* /components/com_blah/blah.php* Purpose: setup your component - defines, includes, etc* Initialize the right controller for the page* Start executing whatever is requested* (This) code expects query string “task” param to be set* If execute called with no param, “display” will be default

22Saturday, August 6, 11

Page 23: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

Component Controllers

Fetch model(s) for data manipulation/checks* $model = $this->getModel('mymodel')* Verify something from the model to proceed or cause a redirect* Ex. Load article, check if published, if not, redirect

Instantiate the View* $view = $this->getView(‘viewName’, ‘html’);* Hand any data (model) to it for use * $view->setModel($model); * $view->assignRef(‘var’, $data); // var for the template* Tell the View to do it’s thing * $view->setLayout(‘templateName’); * $view->display();

23Saturday, August 6, 11

Page 24: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

Controller(Fake) Content Example

Get article

Check if published

Load the view and prepare the template

By default, view of same name as controller will be called

Otherwise, use $view code from previous slide

24Saturday, August 6, 11

Page 25: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

Component Model

The model is your database interface

Extends JModel

Has SQL queries for read or write

Model allows you to retrieve and manipulate data simulating higher level database functions

Table class available for row-based read/writes

$this->_db is pre-set as database object

25Saturday, August 6, 11

Page 26: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

Fetch the data needed from Model

Set data for template file

$this->assignRef('varName', $var);

Call the template to load

Component View

26Saturday, August 6, 11

Page 27: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

Component Template

Little/no code other than echo's

Should use only variables set from View

<?php echo $this->varName ?>

If followed, template overrides become simple

If not, life becomes a nightmare for your clients

27Saturday, August 6, 11

Page 28: Intro to Joomla Development

SourceCoast Extensions sourcecoast.com Intro To Joomla Development

PluginsJDocument

JLogJFormOh My!

28Saturday, August 6, 11