Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/chronocm/public_html/libs/str.php on line 17
ChronoCMS - Simple and powerful CMS - Hello World Extension

Hello World Extension

I will try to shed some light on the new framework/CMS coding by giving some examples, our first example will be the "Hello World extension", we will demonstrate how to create a simple extension and we will also cover other basic topics like system namespaces auto loading.

The first step is to create folder and name it "hello_world", then inside this folder, create 2 folders, name one of them "admin", and the other one "front", create a new XML file and name it "install.xml", copy the code below into your install.xml file:

<!--?xml version="1.0" encoding="utf-8"?-->
<addon type="extension">
  <name>hello_world</name>
  <title>Hello World</title>
  <creationdate>Today</creationdate>
  <author>Me</author>
  <copyright>my name - company</copyright>
  <authoremail>me@email.com</authoremail>
  <authorurl>www.domain.com</authorurl>
  <version>1.0</version>
  <license>GPL</license>
  <description>Hello World extension for ChronoCMS.</description>
</addon>
 

Save the file, then get into the "front" folder, create a new PHP file and name it "hello_world.php", inside this file, insert the code below:

<?php
namespace GCore\Extensions\HelloWorld;
defined("GCORE_SITE") or die;
class HelloWorld extends \GCore\Libs\Controller {
 
  function index(){
    echo "Hello World!";
  }
}
?>
 

Save the file then go back to the "hello_world" folder, select all contents (admin + front + install.xml) and compress to .zip, go to the CMS admin area > Addons > Install, select your newly created extension and hity "Upload and install", tada, the extension should be installed now and can be accessed under any of these paths:

  1. domain.com/index.php?ext=hello_world
  2. domain.com/hello_world**

**this path may not work if you have a page with alias "hello_world" since it will override the extension's default page.

You should see "Hello World!" in the page body once you hit the URL above!

Now let's explain what we have done above :)

The XML file contents should be clear, we define the addon type and some basic info for the CMS, you can omit most of these info and only keep this:

<!--?xml version="1.0" encoding="utf-8"?-->
<addon type="extension">
  <name>hello_world</name>
  <title>Hello World</title>
  <version>1.0</version>
</addon>
 

Next we created the default Extension's controller, here are some notes on this:

  • Any class in your addon should always has a namespace, this namespace should always start with GCore.
  • If the class is under the "front" folder then the namespace should be GCore\Extensions\ExtenionName, and for "admin" classes, it should be GCore\Admin\Extensions\ExtenionName where "ExtenionName" is the camilized version of the real extension name, hence we had "HelloWorld"
  • Since this is the default extension's controller (extensions may have sub controllers), the class name should be the camilized version (ExtensionName), any controller class should extend the \GCore\Libs\Controller (or the \GCore\Libs\GController for more built in features)
  • Since we are testing the extension and need the minimal functionality, we used the default "index" function to output a simple "Hello World" message, other functions can be accessed by appending &act=FN_NAME to the url, try to add a new function called "test" and access it through:
    domain.com/index.php?ext=hello_world&act=test

In this test we used the "Controller" to send output, which is not the best practice of course, let's see how we can send output the correct way, using views, create a new folder under the "front" directory and name it "views", inside the "views" folder create a new file "index.php", and add the content below:

 
<?php defined("GCORE_SITE") or die; ?>
Hello world view file.
 

Save the file and test the extension's page again, you should now see the output of your new file.

View files names should match functions names, a function named "test" will look for a view file named "test.php", moreover, functions starting with underscores "_" can't be accessed through the url and will not load any view files, they are considered protected.

The line below:

<?php defined("GCORE_SITE") or die; ?>
is supposed to prevent direct access to the file, it's a security measurement for the whole system.

Controllers can auto load Models and/or System libraries, Views can auto load Helpers (But through the controller), you can use any of the system built in models, libraries or helpers OR you may develop your own, we will cover this in another article.