DEV Community

M. Alexandre J-S William ELISÉ
M. Alexandre J-S William ELISÉ

Posted on • Originally published at alexandree.io on

API CONSOLE - Jiji, your new Joomla! 4 Console friend

Jiji, your new Joomla! 4 Console friend

System - Jiji

Jiji is a System Plugin leveraging the Joomla! 4 Web Services by adding custom actions to core Joomla! console without “hacks” using DI Container and other Joomla! 4 advanced capabilities.

Edit: After searching later on the web I found out that Chris Davenport already did something similar I think for Joomla 3 at that time. Joomla 4 didn't exists yet.

Download Contact Alexander

WHY ?

I started Jiji back in December 2020 because at that time I wanted some kind of proof of concept, a toy project to try out the new Joomla! 4 console which is based on Symfony Console. If you already know Symfony Console you will be at home with Joomla! 4 Console.

WHAT ?

“Jiji” name comes from what I thought might be a “cute girl” name based on a short nickname version of our beloved “Joomla!” which is a trademark of Open Source Matters by the way.

HOW ?

Jiji is a System Plugin which “adds” or “registers” new commands to the default joomla console cli script JPATH_ROOT/cli/joomla.php

I could create my own console script for that named JPATH_ROOT/cli/jiji.php but I thought it might be better to have only one entrypoint for joomla cli which make it easier to remember. You will tell me if I am wrong. Willing to hear your feedback on that.

Download Contact Alexander

The “Jiji” code is in a Library placed in the src directory inside the plugin.

it is namespaced as AE\Library\Jiji . Namespace are available since PHP 5.3

The Behavior directory holds all the PHP Traits used by the Plugin. Traits are available since PHP 5.4. Basically, they are common behaviors used in unrelated Php Classes. In other words, when you find yourself copy/pasting again and again a functionality from one Class to another, it might be a good candidate for a Trait.

The heart of this plugin is in the AE\Library\Jiji\Console namespace. Is the corresponding directory you will find my first Console Command named HelloSuperJoomlerCommand.php which just says “Hello Super Joomler”. But another directory more interesting is Article directory where are all Article related Commands.

  • GET all articles (Browse)
  • GET one article by id (Read)
  • PATCH one article by id (Edit)
  • POST one article by id (Add)
  • DELETE one article by id (Delete)

In order to DELETE you must first do a PATCH with at minimum the category id , the title and the state as -2 of the article you want to DELETE.

Example JSON payload: ‘{“catid”:64, “title”:“My edited title”, “state”:-2}’

“Jiji” can use a JSON file or JSON string as payload for your requests containing a body.

INSTRUCTIONS:

1 - The extension zip is in the build/ directory of this Github repository

2 - Install the Jiji plugin as any other Joomla! 4 extension.

3 - Follow these instructions

JPATH_ROOT : the root directory of your Joomla 4 website. Change this with the relevant directory absolute path

J4X_BASE_PATH : Your Joomla! 4 Base Url (eg: https://example.com)

J4X_API_TOKEN : Your Joomla! 4 API Token

You can use it non-interactive by adding the -n

Execute the basic Command Hello Super Joomler


php JPATH_ROOT/cli/joomla.php jiji:hello  


Enter fullscreen mode Exit fullscreen mode

Execute the Browse Article Command


php JPATH_ROOT/cli/joomla.php -n --base-path=J4X_BASE_PATH --api-token=J4X_API_TOKEN article:browse  


Enter fullscreen mode Exit fullscreen mode

Execute the Read Article Command (eg: id=1)


php JPATH_ROOT/cli/joomla.php -n --base-path=J4X_BASE_PATH --api-token=J4X_API_TOKEN article:read --id=1  


Enter fullscreen mode Exit fullscreen mode

Execute the Add Article Command

using a JSON string as payload


php JPATH_ROOT/cli/joomla.php -n --base-path=J4X_BASE_PATH --api-token=J4X_API_TOKEN article:add --item-data='{"alias": "my-article","articletext": "My text","catid": 64,"language": "*","metadesc": "","metakey": "","title": "Here's an article"}'  


Enter fullscreen mode Exit fullscreen mode

or more conveniently using a JSON file as payload


php JPATH_ROOT/cli/joomla.php -n --base-path=J4X_BASE_PATH --api-token=J4X_API_TOKEN article:add --item-data='path/to/add-article.json'  


Enter fullscreen mode Exit fullscreen mode

Execute the Edit Article Command (eg: id=1)

using a JSON string as payload


php JPATH_ROOT/cli/joomla.php -n --base-path=J4X_BASE_PATH --api-token=J4X_API_TOKEN article:edit --id=1 --item-data='{"catid": 64,"title": "Here's an another article"}'  


Enter fullscreen mode Exit fullscreen mode

or more conveniently using a JSON file as payload


php JPATH_ROOT/cli/joomla.php -n --base-path=J4X_BASE_PATH --api-token=J4X_API_TOKEN article:edit --id=1 --item-data='path/to/edit-article.json'  


Enter fullscreen mode Exit fullscreen mode

Execute the Delete Article Command (eg: id=1)

This must be done in two steps due to this new way to do it

1 - Execute the Edit Article Command on the article you want to delete by change it’s state to -2 (Trash)

2 - Execute the Delete Article Command on the article you want to delete.

Step 1 for Deleting Article

using a JSON string as payload


php JPATH_ROOT/cli/joomla.php -n --base-path=J4X_BASE_PATH --api-token=J4X_API_TOKEN article:edit --id=1 --item-data='{"catid": 64,"title": "Here's an another article", "state": -2}'  


Enter fullscreen mode Exit fullscreen mode

or more conveniently using a JSON file as payload


php JPATH_ROOT/cli/joomla.php -n --base-path=J4X_BASE_PATH --api-token=J4X_API_TOKEN article:edit --id=1 --item-data='path/to/delete-step-1-article.json'  


Enter fullscreen mode Exit fullscreen mode

Step 2 Deleting Article


php JPATH_ROOT/cli/joomla.php -n --base-path=J4X_BASE_PATH --api-token=J4X_API_TOKEN article:delete --id=1  


Enter fullscreen mode Exit fullscreen mode

CONTRIBUTORS

Contributors are welcomed to jump in and help improve this project. Any constructive feedback is welcomed.

Download Contact Alexander

Top comments (0)