Good day,
Introduction
I have been writing under Joomla for about a year and a half and I always started with a ready-made Joomla CMS package. Developed extensions for it and didn’t blow. Periodically I met in the internet a mention about some Joomla Framework, but did not see anything with its use besides the well-known CMS and its add-ons. The situation turns out to be funny, not a CMS written on the Framework, but a Framework written under the CMS. What is it?
Documentation
Looking for the Quick Start Guide, I visited the
Joomla! Platform website and found an article describing the creation of a Stand Alone Application. This is not exactly what I need. But there was also a link to a couple of examples. It was from
them that I decided to make a start.
Start
Unpack the libraries folder from the archive with the
platform to the root of our site. Next puts unpacked
myWebApp . Without making any additional gestures we launch.
')
Fatal error: Class 'JLog' not found in Z: \ home \ jframework.local \ www \ libraries \ joomla \ environment \ request.php on line 572and on the stack we see the JRequest :: clean () function
It's a shame. But, we recall that in new versions of CMS Joomla at every step (thanks to PhpStorm) a reminder that JRequest @ deprecated in the new version of the platform will be removed. Download the previous version of the platform (11.1), replace the libraries folder and voila. Works.
What's inside?
Not our WebApp consists of two files:
/index.php
/includes/application.phpwe start like this:
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
//
define('JPATH_BASE', dirname(__FILE__));
define('JPATH_PLATFORM', JPATH_BASE . '/libraries');
define('JPATH_MYWEBAPP',JPATH_BASE);
//
require_once JPATH_PLATFORM.'/import.php';
// -
jimport('joomla.environment.uri');
jimport('joomla.utilities.date');
//
jimport('joomla.application.helper');
$client = new stdClass;
$client->name = 'mywebapp';
$client->path = JPATH_MYWEBAPP;
JApplicationHelper::addClientInfo($client);
// JApplication
$config = Array ('session'=>false);
$app = JFactory::getApplication('mywebapp', $config);
//
$app->render();
second file application.php
defined('JPATH_PLATFORM') or die;
final class JMyWebApp extends JApplication
{
public function render()
{
echo ' My Web Application ';
echo 'The current URL is '.JUri::current().'<br/>'; //
echo 'The date is '. JFactory::getDate('now'); //
}
}
That's all, but somehow not at all interesting. Let's add simple logging. To do this, set the logger in the class constructor:
function __construct()
{
$options = array(
'logger' => 'formattedtext',
'text_entry_format' => '{DATE}' . chr(9) . '{TIME}' . chr(9) . '{PRIORITY}' . chr(9) . '{CATEGORY}' . chr(9) . '{MESSAGE}',
'text_file_path' => JPATH_BASE,
'text_file' => 'log.php'
);
$category = array('myApp');
Jlog::addLogger($options, JLog::ALL, $category);
}
now you can log
JLog::add('Test message!', JLog::ALERT, 'myApp');
Let's try to work with the database. To do this, you need to ask it. Having rummaged a little in the code we find that by default the config will be taken from the libraries folder. In it we find config.example.php, by the way. Rename it to config.php and remove unnecessary settings from it, leaving only the DB.
class JConfig
{
public $dbtype = 'mysql';
public $host = 'localhost';
public $user = 'root';
public $password = '';
public $db = 'j16';
public $dbprefix = 'test_';
}
don't forget to rename JConfigExample to JConfigLet's try to work with JTable, Joomla ORM so to speak.
Create a table with two columns in the database
CREATE TABLE `test_test` (
'id' int(8) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
Create the tables folder in the root and the test.php file in it
defined('JPATH_PLATFORM') or die;
class TableTest extends JTable
{
var $id;
var $name;
function __construct( &$_db )
{
parent::__construct( '#__test', 'id', $_db );
}
}
do not forget to connect
require_once JPATH_BASE . '/tables/test.php';
add to the render function
$db = & JFactory::getDbo();
$test = new TableTest($db);
$test->name = 'First name';
$test->store();
after launch we see the error
Fatal error: Class 'JTable' not found in ...It means you need to import. Plus, the line in the index.php file, there to the rest of the imports:
jimport('joomla.database.table');
After starting, we see that in the database, a new entry has appeared in the table.
Conclusion
Hence, it is not so far to develop web applications using the MVC paradigm. It remains only to connect the JModel, JView, JController and forth.