Skip to content

Instantly share code, notes, and snippets.

@renekreijveld
Last active October 24, 2022 03:00
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save renekreijveld/8823105 to your computer and use it in GitHub Desktop.
Save renekreijveld/8823105 to your computer and use it in GitHub Desktop.
Example CLI script for Joomla 3,2
<?php
/**
* @package Joomla.Cli
*
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*
* Joomla 3.2 example CLI script
* Written by: Rene Kreijveld, email [at] renekreijveld.nl
* 05-feb-2014
* Put this script in the /cli folder in the root of your Joomla 3.2 website
* Execute by php <path_to_your_joomla_root>/cli/clidemo_3.2.php
*/
// Set flag that this is a parent file.
const _JEXEC = 1;
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', 1);
// Load system defines
if (file_exists(dirname(__DIR__) . '/defines.php'))
{
require_once dirname(__DIR__) . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', dirname(__DIR__));
require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_LIBRARIES . '/import.legacy.php';
require_once JPATH_LIBRARIES . '/cms.php';
/**
* @package Joomla.CLI
* @since 3.0
*/
class Clidemo extends JApplicationCli
{
/**
* Entry point for CLI script
*
* @return void
*
* @since 3.0
*/
public function doExecute()
{
// Database connector
$db = JFactory::getDBO();
$this->out('Setting query');
// Set SQL query
$query = "SELECT COUNT(*) from #__users";
// Execute query
$db->setQuery($query);
// Get result
$number_of_users = $db->loadResult();
$this->out('Query done');
// Output result
$this->out("We found $number_of_users users.");
}
}
// Instantiate the application object, passing the class name to JCli::getInstance
// and use chaining to execute the application.
JApplicationCli::getInstance('Clidemo')->execute();
@leoli-dev
Copy link

It works, thank you!

@bavamca
Copy link

bavamca commented Apr 14, 2020

HI, How to call my component helper functions to the above cli method ?

@malchikovma
Copy link

Thank you. It's hard for me to find a good Joomla code examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment