joomla extensions kung fu
DESCRIPTION
My presentation at Joomla and Beyond 2010 Conference about popular mistakes made by Joomla extension developers.TRANSCRIPT
Why you can believe me
4 years of experience
Agenda
DevelopmentDevelopment DesignDesign
MVCMVC
Other important questions
Other important questions
Code optimizationCode optimization
The most popular mistakes!
Joomla Components development
Model-View-Controller
How many timesdid you see this?
ModelModel
ViewView Controller
Controller
UserUser
updates manipulates
sees uses
An application is split by 3 departments which collaborate with each other to bring you the best results, but they don’t “disturb each other” at the same time!
MVC Controller
The controller isthe boss!
You should use a controller for
Getting request data;
Getting model & view
instances;
Updating model states;
Calling model action
methods (e.g. delete, save,
remove);
Calling view methods to
render a template.
You should not use a controller for
Database task;
Manipulating model data;
Rendering templates.
model & view tasks
A Controller in Joomla
JController
display() method revealed
/libraries/joomla/application/component/controller.php
Gets view instance
Gets view instance
Gets model
instance
Gets model
instance
Injects model
into view
Injects model
into view
Calls view’s
display() method
Calls view’s
display() method
Bad approaches for usingcontrollers in Joomla
Good approaches for usingcontrollers in Joomla
Bad approaches for usingcontrollers in Joomla
Good approaches for usingcontrollers in Joomla
MVC Model
A model is a hard worker!
You should use a model for
Storing a state;
Performing database tasks;
Formatting data;
All other data task.
You should not use a model for
Getting request data;
Rendering templates.
controller & view tasks
A Model in Joomla
JModel
The most unused model methods & properties.Why?
$this->_db
$this->_state
$this->getDBO();
$this->setState($property,
$value);
$this-
>getState($property);
Bad approaches for usingmodels in Joomla
Good approaches for usingmodels in Joomla
Fat Model, Skinny Controller http://blog.astrumfutura.com/archives/373-The_M_in_MV
C_Why_Models_are_Misunderstood_and_Unappreciated.html
MVC View
A view is a stylist!
You should use a view for
Rendering templates;
Rendering templates;
Rendering templates.
You should not use a view for
Database tasks;
Database tasks;
Database tasks.
model tasks
A view in Joomla
JView
lies between a model and a template
ViewViewDataData TemplateTemplate
You should use a Joomla view class for
Getting data from models;
Rearranging data (if needed);
Assigning variables to
templates;
Rendering templates.
Don’t put everything you have into a view!
Keep your view methods small!
You shouldn’t use PHP in template files like this
You should use it like this
Don’t use variables in view templates other than the ones you assigned to
Keep your templates clear, assign all variables in view classes!
Add CSS and JS in the view template file
… in this case they can be overwritten in a Joomla template
Joomla MVC revealed
Joomla MVC default task as it is
Main component’s file
Main component’s file
request
data
calls
Controller
Controller
defaulttask
calls
JController’s display()
JController’s display()
Model & view objects
Model & view objects
gets
inserts
Model into view
Model into view
callsJView’s display()
JView’s display()
getsModel data
Model data
rendersTemplate
Template
The best way to figure out the Joomla framework is to dig into the code!
No, seriously
Just diginto the code
After digging JController methods you can find out
/libraries/joomla/application/component/controller.php
How to insert other than the default model into
your view class
How to add another location for your model & view
filesHow to set a default model state
…and many more
Joomla MVC structures possible variants
Base controller approach
Base controller approach:main component file
Code Optimization
Code Optimization
Write code
Write code Go!Go!
Write a bit more code
Write a bit more code
Think over
Think over
A simple optimization
Edit, publish, unpublish, save order, order up, order down, cancel, save, remove
Edit, publish, unpublish, save order, order up, order down, cancel, save, remove
Base admin modelBase admin model
Base admin controllerBase admin controller
Base admin viewBase admin view
Helper classesHelper classes
Get ordering, save order, move, check out, check in, publish, remove
Get ordering, save order, move, check out, check in, publish, remove
The most used variables: option, document, view, controller, layout, etc.
The most used variables: option, document, view, controller, layout, etc.
Everything that can be reused in templates
Everything that can be reused in templates
Code Documentation
Document your code
http://www.phpdoc.org/ http://code.google.com/p/jsdoc-toolkit/
Every file, class, class property and method
Filter input, escape output!http://shiflett.org/blog/2005/feb/my-top-two-php-security-practices
http://developer.joomla.org/security.html
http://developer.joomla.org/security/articles-tutorials/258-preventing-sql-injections.html
Joomla Components design
We are developers,we don’t do design!
!!!!!We are developers, we don’t do design !!!!
Design principlesfor developers
Set proper CSS classes &
IDs, but leave design for
template designers
That’s all!
Thanks for coming!
http://twitter.com/
onesterov
http://onesterov.com
http://jookungfu.org
http://mindk-lab.com