4

I am writing a cronjob (cron.php) which will have to run under the Joomla root folder which call Joomla functions like JFactory::getDbo().

I have the following code in cron.php

define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );
require( JPATH_BASE.'/libraries/import.php');
$db = JFactory::getDbo();

I got the error

atal error: Class 'Joomla\Registry\Registry' not found in D:\x\htdocs\xxx\libraries\joomla\factory.php on line 558
4

Firstly, you're importing the wrong files which are required when using the Joomla API.

You then also need to initialise the application.

try the following:

define('_JEXEC', 1);
define('JPATH_BASE', dirname(__FILE__));

require_once JPATH_BASE '/includes/defines.php';
require_once JPATH_BASE '/includes/framework.php';

$mainframe = JFactory::getApplication('site');
$mainframe->initialise();

As for the actual cron job, I would suggest placing the PHP file in the CLI directory rather than the root, as this is where they're supposed to go.

Take a look at some of the files already in that directory to see how they're set up.

Something I forgot to point out, If you would like to ensure your cron job is only executable via command line, you should add the following to the top of the PHP file:

if (PHP_SAPI !== 'cli')
{
    die('This is a command line only application.');
}
  • Do we really need the get application and do initialise? It works even without that.. Any specific reason? +1 for directing me to CLI folder... – Malaiselvan Sep 23 '15 at 21:08
0
<?php

define('_JEXEC', 1);
defined('DS') or define('DS', DIRECTORY_SEPARATOR);

if (file_exists(__DIR__ . '/defines.php'))
{
    include_once __DIR__ . '/defines.php';
}

if (!defined('_JDEFINES'))
{
    define('JPATH_BASE', __DIR__);
    require_once JPATH_BASE . '/includes/defines.php';
}

require_once JPATH_BASE . '/includes/framework.php';
$app = JFactory::getApplication('site');
$admin = '';
if(!$app->isSite()){
    $admin = DS.'administrator';//echo('in administrator');
}
$db = JFactory::getDbo();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.