Using PHP in Joomla
For many developers, portability is a key concept that is always in the back of our
minds when designing a website or using a specific platform for doing our coding. When I
first started playing with Joomla, I was impressed with the amount of features that it had
as well as it's mechanisms for creating 'articles', however I was immediately concerned about
upgradability. Questions like "what if they have a major update, will my articles be
automatically transferred?" or "what if I have a separate area for development work and testing
of a new major version of Joomla, how can i easily get my articles there?". After doing some
browsing and thinking, I soon realized that what many people end up doing is actually creating
their articles within a php document, and then having Joomla parse them with some 3rd party
plugins.
There are several benefits to using PHP with Joomla.
Before we do anything, we first need a plugin for Joomla that will make it easier to integrate
php code with Joomla articles. I used to use the Jumi plugin which was great, but since upgrading
to Joomla 3.0 I have switched over to Sourcerer.
Once you install sourcerer, you can easily integrate php code right inside of our articles. Using a
simple tag, you can start using any type of source code that you need, either calling php code
directly, or importing php files. In my articles, I actually use an import statement like the
following.
There are a few tips that you should use to get started with your php coding. There are certain
Joomla calls and libraries that should be used to ensure that your site is properly formatted and
follows the code pattern of the Joomla system. There are mechanisms built in to help users import
and use javascript and style sheets. To get started, you should put the following at the top of
your php file.
The code above does a few things. First, it protects your PHP file from direct access. Even if you
do not have anything in the php file that you want to hide from the public, it is a habit you want
to get into. This ensures that the Joomla environment is properly initialized and that your code is
running within the environment. The second line of the code simply gets the document object and stores
it in a local variable.
Assuming that you have been following the tutorial so far, you should be all set to start using Joomla
libraries and their functions. One of the most powerful functions is its scripting libraries. For
example, say you want to use javascript in your php file, but you want to make sure you arent using a
script that has already been imported, and you want your script tags to be orgainzed in your site. Well,
Joomla provides a nice and easy way to do that. Lets take a look at how I have imported and instantiated
the libraries used to create the source code display below.
By using 'addScript' we are telling the Joomla framework that we want to import a javascript file that is
found at 'scripts/...js' and is relative to the base url of our site. Not only does the Joomla library
make sure that you haven't imported a library twice, but makes sure to place all of the script's together
at the head of the document. Cool huh!
Let's say that we need to manipulate some javascript variables in the libraries we imported, well instead
of manually entering script tags, we can do the following:
This time we used 'addScriptDeclaration'. Go ahead and right click on this page and view the source, you should
see exactly how the Joomla framework handled these function calls.
Just like with Javascript, Joomla provides some functions for importing and declaring styles and stylesheets.
The calls are similar to what we used for the javascript functions:
Just like with the javascript, Joomla provies an 'addStyleSheet' and 'addStyleDeclaration' call that will
make sure that we haven't imported two of the same CSS files, and will align them properly in the page's
source code.
PHP Coding With Joomla
Benefits To PHP Coding For Joomla
First Things First, Get Sourcerer
{source}<?php
require_once JPATH_SITE.'/myphpcodefolder/somephpfile.php';
?>{/source}
Tips to getting started
<?php
defined('_JEXEC')
OR defined('_VALID_MOS') OR die( "Direct Access Is Not Allowed" );
$doc = JFactory::getDocument();
.
.
.
?>
Using Javascript in your PHP File
$doc->addScript(JURI::base() . 'scripts/shCore.js', 'text/javascript');
$doc->addScript(JURI::base() . 'scripts/shBrushPhp.js', 'text/javascript');
$doc->addScriptDeclaration('
SyntaxHighlighter.config.clipboardSwf = "/scripts/clipboard.swf";
SyntaxHighlighter.defaults["auto-links"] = true;
SyntaxHighlighter.defaults["collapse"] = false;
SyntaxHighlighter.defaults["gutter"] = true;
SyntaxHighlighter.defaults["smart-tabs"] = true;
SyntaxHighlighter.defaults["tab-size"] = 4;
SyntaxHighlighter.defaults["toolbar"] = true;
SyntaxHighlighter.defaults["wrap-lines"] = true;
SyntaxHighlighter.all();
');
What about CSS Files
$doc->addStyleSheet(JURI::base() .
'libraries/codecitation/codecitation/styles/shCore.css',
$type = 'text/css',
$media = 'screen,projection');
$doc->addStyleSheet(JURI::base() .
'libraries/codecitation/codecitation/styles/shThemeDefault.css',
$type = 'text/css',
$media = 'screen,projection');
$doc->addStyleDeclaration('
table.datatable tbody tr td p {
font: 11px "Courier New";
}
');